如何控制一个线程的超时?
void *thread1(void)
{
printf("thread1 : I`m thread 1 \n");
for (i=0; i<MAX; i++)
{
printf("thread1 : before global share number = %d \n",number);
printf("thread1 : locking...\n");
pthread_mutex_lock(&mut); // 上锁
sleep(1);
number++;
pthread_mutex_unlock(&mut); // 解锁
printf("thread 1 is unlocked!\n");
printf("thread 1 : after global share number = %d \n",number);
}
printf("pthread 1 will exit...\n");
pthread_exit(NULL);
}
。。。。。
memset(&thread,0,sizeof(thread));
if((tmp = pthread_create(&thread[0],NULL,(void *)thread1,NULL)) != 0)
{
printf(" pthread 1 creating failed!\n");
}
else
{
printf(" pthread 1 creating successfully!\n");
}
。。。。。
if (thread[0] != 0)
{
pthread_join(thread[0],NULL); // 等待线程结束
printf("thread 1 completion!\n");
}
[解决办法]
关注。。。