#include
#include
#include
#include
#define NUM_THREADS 8
void *PrintHello(void *args)
{
int thread_arg;
sleep(1);
thread_arg = (int)(*((int*)args));
printf("Hello from thread %d\n", thread_arg);
return NULL;
}
int main(void)
{
int rc,t;
pthread_t thread[NUM_THREADS];
for( t = 0; t < NUM_THREADS; t++ )
{
printf("Creating thread %d\n", t);
rc = pthread_create(&thread[t], NULL, PrintHello, &t);
if (rc)
{
printf("ERROR; return code is %d\n", rc);
return EXIT_FAILURE;
}
}
sleep(5);
for( t = 0; t < NUM_THREADS; t++ )
pthread_join(thread[t], NULL);
return EXIT_SUCCESS;
}
这段程序为什么都是输出Hello from thread 8,而不是0-7