线程等待

来源:互联网 发布:电脑版软件 地图 编辑:程序博客网 时间:2024/05/20 11:23
void *func(void *args)
{
Sleep(2);
printf("this is func!\n");
}

void main()
{
pthread_t pid;
if(pthread_create(&pid, NULL, func, NULL))
{
return -1;
}

/*
用于等待一个线程的结束
如果代码中没有pthread_join,主线程会很快结束,从而使整个进程结束,从而使创建的线程pid没有机会
开始执行就结束了
*/
pthread_join(pid, NULL);
printf("this is end of main!\n");
return;

}

编译:

gcc wait.c -o wait -lpthread

原创粉丝点击