linux下的C语言开发(线程等待)
2012-02-28 17:38
232 查看
和多进程一样,多线程也有自己的等待函数。这个等待函数就是pthread_join函数。那么这个函数有什么用呢?我们其实可以用它来等待线程运行结束。
[cpp]
view plaincopyprint?
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void func(void* args)
{
sleep(2);
printf("this is func!\n");
}
int main()
{
pthread_t pid;
if(pthread_create(&pid, NULL, func, NULL))
{
return -1;
}
pthread_join(pid, NULL);
printf("this is end of main!\n");
return 0;
}
[cpp]
view plaincopyprint?
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void func(void* args)
{
sleep(2);
printf("this is func!\n");
}
int main()
{
pthread_t pid;
if(pthread_create(&pid, NULL, func, NULL))
{
return -1;
}
pthread_join(pid, NULL);
printf("this is end of main!\n");
return 0;
}
[cpp] view plaincopyprint? [test@localhost thread]$ ./thread this is func! this is end of main! [test@localhost thread]$ ./thread this is func! this is end of main!
相关文章推荐
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(进程创建及等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- 嵌入式Linux应用程序开发——多线程1(线程的创建和等待)
- Linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- linux 线程等待与退出 pthread_join pthread_exit 函数
- 服务器端开发经验总结 (Linux C语言) – 1
- Linux下c开发 之 线程通信
- linux下的C语言开发
- linux下线程的创建和等待