得られた結果のために、 pthread_join() 関数を理解するのに苦労しています。
pthread_join() が、指定されたスレッド ID のスレッドが終了するまで呼び出しスレッドを一時停止することになっている場合、次のコードが最初にスレッド 1を実行せず、次にスレッド 2を実行するのはなぜですか。それらは両方同時に起こっています。
(main から) 2 つの pthread_join() 行を取り出すと、プログラムは終了し、何も起こりません。これは、メイン スレッドが両方の結合関数の呼び出しプロセスであり、新しく作成された他の 2 つのスレッドが終了するのを待っているのはメイン スレッドであることを意味しますか?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *functionCount1();
void *functionCount2(void*);
int main()
{
/*
How to Compile
gcc -c foo
gcc -pthread -o foo foo.o
*/
printf("\n\n");
int rc;
pthread_t thread1, thread2;
/* Create two thread --I took out error checking for clarity*/
pthread_create( &thread1, NULL, &functionCount1, NULL)
pthread_create( &thread2, NULL, &functionCount2, &thread1)
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("\n\n");
exit(0);
}
void *functionCount1()
{
printf("\nFunction 1");
sleep(5);
printf("\nFunction 1");
return(NULL);
}
void *functionCount2(void* argument)
{
//pthread_t* threadID = (pthread_t*) argument;
//pthread_join(*threadID, NULL);
printf("\nFunction 2");
sleep(5);
printf("\nFunction 2");
return(NULL);
}
出力:
sleep
コメントアウトした出力:
なぜ pthread_join がドキュメンテーションであなたが信じていることをしていないのか、誰か説明できますか?