pthread_cond_wait()的使用方法
来源:互联网 发布:淘宝企业店铺转为个人 编辑:程序博客网 时间:2024/05/03 09:00
pthread_cond_wait
编辑
条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
- 多线程的条件变量
- pthread_cond_wait
- 条件变量的条件成立
- "条件成立"(给出条件成立信号)
- 为了防止竞争
- 和一个互斥锁结合在一起
目录
多线程的条件变量[1]
条件变量和互斥锁一样,都有静态动态两种创建方式,静态方式使用PTHREAD_COND_INITIALIZER常量,如下:
pthread_cond_t cond=PTHREAD_COND_INITIALIZER
动态方式调用pthread_cond_init()函数,API定义如下:
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)
尽管POSIX标准中为条件变量定义了属性,但在LinuxThreads中没有实现,因此cond_attr值通常为NULL,且被忽略。
注销一个条件变量需要调用pthread_cond_destroy(),只有在没有线程在该条件变量上等待的时候才能注销这个条件变量,否则返回EBUSY。因为Linux实现的条件变量没有分配什么资源,所以注销动作只包括检查是否有等待线程。API定义如下:
int pthread_cond_destroy(pthread_cond_t *cond)
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
等待条件有两种方式:条件等待pthread_cond_wait()和计时等待pthread_cond_timedwait(),其中计时等待方式如果在给定时刻前条件没有满足,则返回ETIMEOUT,结束等待,其中abstime以与time()系统调用相同意义的绝对时间形式出现,0表示格林尼治时间1970年1月1日0时0分0秒。
无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求pthread_cond_wait()(或pthread_cond_timedwait(),下同)的竞争条件(Race Condition)。mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁(PTHREAD_MUTEX_ADAPTIVE_NP),且在调用pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应。
激发条件有两种形式,pthread_cond_signal()激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;而pthread_cond_broadcast()则激活所有等待线程。
现在来看一段典型的应用:看注释即可。
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
static
pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;
static
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
struct
node {
int
n_number;
struct
node *n_next;
} *head=NULL;
/*[thread_func]*/
/*释放节点内存*/
static
void
cleanup_handler(
void
*arg) {
printf
(
"Clean up handler of second thread.\n"
);
free
(arg);
(
void
)pthread_mutex_unlock(&mtx);
}
static
void
*thread_func(
void
*arg) {
struct
node*p=NULL;
pthread_cleanup_push(cleanup_handler,p);
pthread_mutex_lock(&mtx);
//这个mutex_lock主要是用来保护wait等待临界时期的情况,
//当在wait为放入队列时,这时,已经存在Head条件等待激活
//的条件,此时可能会漏掉这种处理
//这个while要特别说明一下,单个pthread_cond_wait功能很完善,
//为何这里要有一个while(head==NULL)呢?因为pthread_cond_wait
//里的线程可能会被意外唤醒,如果这个时候head==NULL,
//则不是我们想要的情况。这个时候,
//应该让线程继续进入pthread_cond_wait
while
(1) {
while
(head==NULL) {
pthread_cond_wait(&cond,&mtx);
}
//pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,
//然后阻塞在等待队列里休眠,直到再次被唤醒
//(大多数情况下是等待的条件成立而被唤醒,唤醒后,
//该进程会先锁定先pthread_mutex_lock(&mtx);,
//再读取资源用这个流程是比较清楚的
/*block-->unlock-->wait()return-->lock*/
p=head;
head=head->n_next;
printf
(
"Got%dfromfrontofqueue\n"
,p->n_number);
free
(p);
}
pthread_mutex_unlock(&mtx);
//临界区数据操作完毕,释放互斥锁
pthread_cleanup_pop(0);
return
0;
}
int
main(
void
) {
pthread_t tid;
int
i;
struct
node *p;
pthread_create(&tid,NULL,thread_func,NULL);
//子线程会一直等待资源,类似生产者和消费者,
//但是这里的消费者可以是多个消费者,
//而不仅仅支持普通的单个消费者,这个模型虽然简单,
//但是很强大
for
(i=0;i<10;i++) {
p=(
struct
node*)
malloc
(
sizeof
(
struct
node));
p->n_number=i;
pthread_mutex_lock(&mtx);
//需要操作head这个临界资源,先加锁,
p->n_next=head;
head=p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
//解锁
sleep(1);
}
printf
(
"thread1wannaendthecancelthread2.\n"
);
pthread_cancel(tid);
//关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,
//子线程会在最近的取消点,退出线程,而在我们的代码里,最近的
//取消点肯定就是pthread_cond_wait()了。
pthread_join(tid,NULL);
printf
(
"Alldone--exiting\n"
);
return
0;
}
- 参考资料
0 0
- pthread_cond_wait()的使用方法
- pthread_cond_wait()的使用方法
- pthread_cond_wait()的使用方法
- pthread_cond_signal()和pthread_cond_wait()的典型使用方法
- pthread_cond_wait() 函数的使用
- pthread_cond_wait的工作机制
- pthread_cond_wait()的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()的使用
- pthread_cond_wait()的用法分析
- pthread_cond_wait的思考
- pthread_cond_wait()的实现原理
- pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()的疑惑?
- pthread_cond_wait的锁操作
- pthread_cond_wait的一个例子
- pthread_cond_wait()函数的理解
- setContentView( )方法
- POJ 2609 Ferry Loading(双塔DP)
- 吃了程序员这碗青春饭 40岁你该怎么办?
- 数据科学家应该掌握的12种机器学习算法
- setContentView( )方法
- pthread_cond_wait()的使用方法
- HDU 3578 Greedy Tino(双塔DP)
- 第一个DLL程序:动态链接库DLL教程,30分钟快速上手
- GeoPython 随笔:空间数据查询——测试代码
- 动态链接库DLL的加载:隐式加载(载入时加载)和显式加载(运行时加载)
- win7任务栏图标显示异常的解决方法
- CodeForces 157A Game Outcome
- CodeForces 157B Trace
- CodeForces 156A Message(暴力)