0

ThreadMe クラスの thefunction() という名前の関数のメイン関数にスレッドを作成しようとしています。トリッキーな部分は、別のクラス TYIA でスレッドを開始する必要があることです - Roland

#include <iostream>
#include <process.h>
#include <windows.h>

int main()  {

    char cincatcher[24];

        std::cout << "I want to run a thread using a function on another class\n";

//      Here is a good place to start the thread

        while( true )   {


        std::cin >> cincatcher
    }
}


class ThreadMe  {

    void thefunction();
};

void ThreadMe::thefunction()    {

    while( true )   {

        std::cout << "working!\n"
        Sleep(800);
    }
}
4

1 に答える 1

1

クラスメソッドでスレッドを直接開始することはできません。クラスメソッドを通常の関数にラップしてから、関数でスレッドを開始する必要があります。次のように:

void threadBody(void *p) {
     ThreadME tm;
     tm.thefunction();
}
于 2013-01-17T02:23:10.957 に答える