次のようなスレッド化されたオブジェクトを実装しようとしています:
#include <pthread.h>
#include <iostream>
class Thread
{
private:
int id;
static void * run(void * arg)
{
int tid = (int)arg;
std::cout << "Thread " << tid << " executed." <<std::endl;
pthread_exit(NULL);
}
public:
Thread(int _id);
void start();
int get_id();
};
public メソッドとコンストラクターの実装は次のとおりです。
#include "std_thread.h"
Thread::Thread(int _id)
{
id = _id;
}
void Thread::start()
{
std::cout << "Thread created." <<std::endl;
pthread_t thread;
int rc = pthread_create(&thread, NULL, run, (void*)id);
if(rc)
std::cout << "Return code from thread is " << rc;
}
int Thread::get_id()
{
return id;
}
そして、ここにメインがあります:
#include "std_thread.h"
int main()
{
Thread *thd = new Thread(0);
thd->start();
return 0;
}
スレッド オブジェクトを作成し、その start メソッドを呼び出すと、「スレッドが作成されました」と出力されるはずです。スレッド本体を実行します-実行しません。実際には、Thread created をコンソールに出力しますが、スレッドを作成していないように見えるか、スレッドは何もしません。ちなみに、すべてが正常にコンパイルされ、実行時エラーはありません。
何か案は?