pthread_join
- 摘要:pthread_join使一个线程等待另一个线程束。
代码中如果没有pthread_join主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了。加入pthread_join后,主线程会一直等待直到等待的线程结束自己才结束,使创建的线程有机会执行。
- 函数pthread_join用来等待一个线程的结束。
目录
-
1函数简介
-
2函数应用
- ▪ linux中的应用
- ▪ pthread_join的应用
-
3使用范例
1函数简介编辑
2函数应用编辑
linux中的应用
attributes来设置当一个线程结束时,直接回收此线程所占用的系统资源,详细资料查看Threads attributes。
pthread_join的应用
3使用范例编辑
// 子线程阻塞,等待信号,然后输出字符串
// 主线程从键盘录入字符,给子线程发信号。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <time.h>
pthread_t tid; sigset_t set;
void
myfunc()
{
printf("hello\n");
}
static
void* *p)
{
int
signum;
while(1)
{
sigwait(&set,&signum);
if(SIGUSR1
== signum)
myfunc();
if(SIGUSR2
== signum)
{
printf("I will sleep 2 second and exit\n");
sleep(2000);
break;
}
}
}
int
main()
{
char
tmp;
void*
status;
sigemptyset(&set);
sigaddset(&set,SIGUSR1);
sigaddset(&set,SIGUSR2);
sigprocmask(SIG_SETMASK,&set,NULL);
pthread_create(&tid,NULL,mythread,NULL);
while(1)
{
printf(":");
scanf("%c",&tmp);
if('a'
== tmp)
{
pthread_kill(tid,SIGUSR1);
//发送SIGUSR1,打印字符串。
}
else
if('q'==tmp)
{
//发出SIGUSR2信号,让线程退出,如果发送SIGKILL,线程将直接退出。
pthread_kill(tid,SIGUSR2);
//等待线程tid执行完毕,这里阻塞。
pthread_join(tid,&status);
printf("finish\n");
break;
}
else
continue;
}
return
0;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
void
* start_run(void * arg)
{
//do
some work
}
int
main()
{
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&thread_id,&attr,start_run,NULL);
pthread_attr_destroy(&attr);
sleep(5);
exit(0);
}
|
有,如果线程已经调用pthread_join()后,则再调用pthread_detach()则不会有任何效果。
pthread_join的更多相关文章
- 对线程等待函数pthread_join二级指针参数分析
分析之前先搞明白,这个二级指针其实在函数内部是承接了上个线程的返回值. 看man手册,发现返回值是个普通指针.人家用二级指针来承接,可能准备干大事.这个可以自己搜索一下.原因嘛,二级指针是保存了这个地 ...
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...
- Linux线程-pthread_join
pthread_join用来等待另一个线程的结束,函数原型如下: extern int pthread_join __P ((pthread_t __th, void **__thread_retur ...
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- Linux 线程--那一年, 我们一起忽视的pthread_join
前言: 通过linux的pthread库, 相信大家对创建/销毁线程肯定很熟悉, 不过对pthread_join是否知道的更多呢?实验: 先编写一个常规的程序 #include <pthread ...
- pthread_join和pthread_detach的用法(转)
一:关于join join join是三种同步线程的方式之一.另外两种分别是互斥锁(mutex)和条件变量(condition variable). 调用pthread_join()将阻塞自己,一直到 ...
- 多线程:pthread_exit,pthread_join,pthread_self
/*exit_join_id.c*/ #include<pthread.h> #include<stdio.h> void* eji(void* agr) { printf(& ...
- linux线程之pthread_join和pthread_detach
在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在 被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反 ...
- linux线程之pthread_join
pthread_join使一个线程等待另一个线程结束. 代码中如果没有pthread_join:主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了.加入pthread_joi ...
随机推荐
- git 使用系列(二)---- 分支和合并
Branching and Merging The Git feature that really makes it stand apart from nearly every other SCM o ...
- ant 配置expdp and impap
+ 执行步骤: ant -f 1_exp_prod.xml copy file from prod to uat (maunule) ant -f 3_imp_uat.xml 附件: 1.1_exp ...
- 关于web.xml的格式
先是filter 再是<filter-mapping> 然后<servlet> 再是<servlet-mapping> 这是一种规范基于j2ee 在开发的过程中一 ...
- Windsock套接字I/O模型学习 --- 第二章
1. select模型 select模型主要借助于apiselect来实现,所以先介绍一下select函数 int select( int nfds, // 忽略,仅是为了与 Berkeley 套接字 ...
- HTML day03表格与表单
1.表格 一般格式: <table> <thead><!--表格头--> <tr> <th></th> </tr>& ...
- UVA 11992 线段树
input r c m r<=20,1<=m<=20000 m行操作 1 x1 y1 x2 y2 v add v 2 x1 y1 x2 y2 v s ...
- phpmyadmin导出数据中文乱码问题
phpMyAdmin版本是2.9.1.1 用phpMyAdmin备份数据库,导出来后,在本地用sqlyog还原的时候,出现如下错误:Error occured at:2009-03-03 10:09: ...
- gvim work notes.. a few days' work on 64bit vim and plugin compilations
(a 600MB+ sized c/c++ compiler which is capable of hi-light and JB styled completion!! and of-course ...
- Rar related CMD
recursively add folder Document to archive: (with all the files) rar a *.rar Document recursively ad ...
- 学习笔记——组合模式Composite
组合模式,典型的层次结构. 与装饰器类图相似. 区别在于:装饰器模式是为了在接口中增加方法,而组合模式在于层次元素的叠加. ConcreteComponent就是中间结点,可以包含更多的Concret ...