代码如下:
#include<pthread.h>
#include<stdio.h>
#define MAX_THREADS 5
void *myThread(void *arg)
{
printf("Thread %d started\n", (unsigned int)arg);
pthread_exit(arg);
}
int main()
{
int ret, i, status;
pthread_t threadids[MAX_THREADS];
for(i=0; i<MAX_THREADS; i++)
{
ret=pthread_create(&threadids[i], NULL, myThread, (void*)i);
if(ret!=0)
printf("error creating thread %d\n", i);
}
for(i=0; i<MAX_THREADS; i++)
{
ret=pthread_join(threadids[i], (void** )&status);
if(ret!=0)
printf("error joining thread %d\n", i);
else
printf("status=%d\n",status);
}
return 0;
}
为什么运行结果为:
Thread 3 started
Thread 4 started
Thread 2 started
Thread 1 started
Thread 0 started
status=0
status=1
status=2
status=3
status=4
谢谢!
忽然笑
皈依舞
呼啦一阵风
随时随地看视频慕课网APP