マルチスレッドの仕組みを理解しようとしています。私はこのコードの問題はthread_function1
の実行が完了する前に、メインの実行が完了したことにある次のコードに `並行マルチスレッド
void handler(void *arg)
{
printf("Printf from cleanup handler: %s\n", (char*)arg);
}
void print(const char *msg)
{
printf("%7s: Pid=%d Tid:%lu\n", msg, getpid(), pthread_self());
}
void* thread_function1(void *args)
{
printf("Received: %d\n", (int)args);
print("Thread");
pthread_cleanup_push(handler, "hello");
pthread_cleanup_pop(1);
printf("Thread Done\n");
return (void *) 0;
}
int main(void)
{
pthread_t tid1, tid2;
void *tret;
if(pthread_create(&tid1, NULL, thread_function1, (void *)1))
exit(1);
if(pthread_create(&tid2, NULL, thread_function1, (void *)2))
exit(1);
// pthread_join(tid2, &tret);
// pthread_join(tid1, &tret);
}
を書きました。両方のコメントが削除された場合は、thread 1
の実行が完了した後にのみthread 2
が実行されます。
私がしたいことは、thread 1
とthread 2
が同時に実行され、main
が両方のスレッドの完了を待つ必要があります。
スレッドは(ほんの些細な)作業量をしますか? 2番目のスレッドが作成される前に 'thread_function1'が終了することはありますか? –
@WanderNauta質問を更新しました。それを見てください。これは、スレッド2は、スレッド1が完了した後にのみ実行されたことを意味し – Ibrahim