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;
{
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
本文通过一个简单的C语言程序示例展示了如何使用pthread库创建一个子线程,并通过pthread_join函数确保主线程等待子线程执行完毕。示例程序包含主线程与子线程的交互过程。
2067

被折叠的 条评论
为什么被折叠?



