pthreadを使用してスレッドクラスを実装したいと思います。 もちろん、作成しているスレッドごとに異なる起動ルーチンを用意したいと思います。 pthread_create thoはスタティック関数のみを開始ルーチンとして許可するため、インスタンス化できません。 これを可能にする方法はありますか、スレッドを処理するために構造体を使用する方が良いですか? これは私がSOFAR書いたコードは次のとおりです。Posixスレッドクラスとスタートルーチン(pthread)
class thread {
string name;
pthread_t id;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_attr_t attr;
public:
thread (string t_name);
static void* start(void*);
int id_get();
private:
};
thread::thread (string t_name)
{
name = t_name;
pthread_attr_init(&attr);
int stacksize = sizeof(double) * TH_STACK_SIZE * 30;
pthread_attr_setstacksize(&attr, stacksize);
int rc = pthread_create (&id, &attr, &start, NULL);
cout << "return_code: " << rc << endl;
cout << id;
}
void* thread::start(void*)
{
while(1){
cout << "here";
pthread_exit(NULL);
}
}
int thread::id_get()
{
return id;
}
と主な私のテスト:あなたはのavailble POSIXスレッドを使用している場合
int main(void) {
cout << "Creating threads" << endl;
thread test1("first");
thread test2("second");
pthread_join(test1.id_get(),NULL);
pthread_join(test2.id_get(),NULL);
return 0;
}
既存のstd :: threadを使用しないのはなぜですか? –
原因私はARM用にクロスコンパイルしているので、pthreadははるかに移植性があります(少なくとも私が読んだことです)。 – Podarce
stdは何を表していますか? –