スレッド内でこのコードを実行しているときに、なぜこの回答が得られたのかを理解しようとしています。 なぜ私は毎回違う価値を得られないのか分かりません。 複数のスレッドが一緒に値をプリントする
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *printHello(void *threadid)
{
int tid = *(int*)threadid;
printf("Hello World! It's me, thread #%d!\n", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
int i;
for (i = 0; i < NUM_THREADS; i++)
{
printf("In main: creating thread %ld\n", i,);
rc = pthread_create(&threads[i], NULL, printHello, &i);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
彼らはそれらを作成し終えると、彼らはprintHello機能に行くまで、すべてのスレッドを待機さでしょうか?
ありがとうございました。それは明らかな答えだった –