linux下的C语言开发(线程等待)
2015-10-09 10:21
537 查看
和多进程一样,多线程也有自己的等待函数。这个等待函数就是pthread_join函数。那么这个函数有什么用呢?我们其实可以用它来等待线程运行结束。
[cpp]
view plaincopy
#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;
}
编写wait.c文件结束之后,我们就可以开始编译了。首先你需要输入gcc wait.c -o wait -lpthread,编译之后你就可以看到wait可执行文件,输入./wait即可。
[cpp]
view plaincopy
[test@localhost thread]$ ./thread
this is func!
this is end of main!
[cpp]
view plaincopy
#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;
}
编写wait.c文件结束之后,我们就可以开始编译了。首先你需要输入gcc wait.c -o wait -lpthread,编译之后你就可以看到wait可执行文件,输入./wait即可。
[cpp]
view plaincopy
[test@localhost thread]$ ./thread
this is func!
this is end of main!
相关文章推荐
- Linux命令最全版
- redhat centos yum源的安装
- redhat linux 安装mysql5.6.27
- Linux命令之管道
- 每天一个linux命令(25):linux文件属性详解
- linux中的awk
- [每天学一点Linux系列]Linux常用的命令
- linux常用命令搜索
- linux 系统管理命令
- linux在top命令有哪些情况会导致average变大
- linux 软链接
- Linux伙计专用Hello World Java
- centos7 开机默认进入字符界面
- 浅析Linux系统下安装wetty和使用说明
- Linux主机被黑过程和简单处理
- mv指令
- Linux常用指令
- linux 如何显示和删除隐含文件
- linux中ls命令详解
- ECS Linux开启swap(虚拟内存)