0
我想理解为什么我得到这个答案,而我在线程中运行此代码。 我不明白为什么我没有得到不同的我每次价值。 多线程打印值在一起
#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功能?
谢谢。这是明确的答案 –