代碼:
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#define N 3
void *thrd_func(void *arg)
{
int thrd_num=(int)arg;
int delay_time=1;
int count=0;
printf("thead %d is starting\n",thrd_num);
for(count=0;count<5;count++)
{
// delay_time=(int)(rand()*6.0/(RAND_MAX))+1;
delay_time=thrd_num+1;
sleep(delay_time);
printf("\t thread %d :job %d delay\n",thrd_num,delay_time);
}
printf("thread %d finished\n",thrd_num);
pthread_exit(NULL);
}
int main()
{
pthread_t thread[3];
int no=0,res;
void *thrd_ret;//=malloc(4);
srand(time(NULL));
for(no=0;no<N;no++)
{
res=pthread_create(&thread[0],NULL,thrd_func,(void *)no);
if(res!=0)
{
printf("create %d failed\n",no);
exit(res);
}
}
printf("create threads success!\n");
for(no=0;no<N;no++)
{
// thrd_ret=(void *)1;
res=pthread_join(thread[no],&thrd_ret);
printf("ret:%d turd_ret:%d\n",res,(int)thrd_ret);
if(!res)
{
printf("thread %d joined\n",no);
}
else
{
printf("thread %d joined failed\n",no);
}
}
運行:

除錯:

求各位大神們看看
uj5u.com熱心網友回復:
我找到錯誤了……好氣……轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/99064.html
標籤:非技術區
上一篇:安裝pve6.0
下一篇:MPSOC 如何生成PMU?
