linux下的C语言开发(线程等待)
2012-02-26 16:02
253 查看
和多进程一样,多线程也有自己的等待函数。这个等待函数就是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下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- linux下的C语言开发(线程等待)
- 嵌入式Linux应用程序开发——多线程1(线程的创建和等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- Linux下的C语言开发(进程等待)
- linux下的C语言开发(进程创建及等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- linux下的C语言开发(进程等待)
- Linux环境下段错误的产生原因及调试方法小结 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且项目工程庞大复杂,出现了不少问题,其中遇到最多、花费时间最长的问题就是
- 【Linux】线程(概念、特点、线程控制代码--创建/等待/终止、分离与结合属性)
- 【转】linux下的C语言开发(gdb调试)
- linux下的C语言开发(AT&T 汇编语言)
- linux下的C语言开发(gdb调试)