多线程编程: 条件变量

条件变量 条件变量是利用线程间共享的全局变量进行同步的一种机制, 主要包括两个动做: 一个线程等待"条件变量的条件成立"而挂起; 另外一个线程使"条件成立"(给出条件成立信号). 为了防止竞争,条件变量的使用老是和一个互斥锁结合在一块儿。
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);
2. 等待和激发
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表示格林尼治时间197011000
不管哪一种等待方式,都必须和一个互斥锁配合, 以防止多个线程同时请求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();
html

激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;node

 

pthread_cond_broadcast(); 激活全部等待线程。
如今来看一段典型的应用:看注释便可。
并发

 

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.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;

static void cleanup_handler(void *arg)
{
printf("Cleanup 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);
while (1) 
{
pthread_mutex_lock(&mtx); //这个mutex主要是用来保证pthread_cond_wait的并发性
while (head == NULL) 
{ 
/* 
* 这个while要特别说明一下,
* 单个pthread_cond_wait功能很完善,为什么这里要有一个while (head == NULL)呢?
* 由于pthread_cond_wait里的线程可能会被意外唤醒,若是这个时候head != NULL,则不是咱们想要的状况。
* 这个时候,应该让线程继续进入pthread_cond_wait
*/

/*
* pthread_cond_wait会先解除以前的pthread_mutex_lock锁定的mtx,
* 而后阻塞在等待对列里休眠,
* 直到再次被唤醒(大多数状况下是等待的条件成立而被唤醒,唤醒后,该线程会先锁定pthread_mutex_lock(&mtx);
* 再读取资源;
* 这个流程能够表述为:block-->unlock-->wait() return-->lock.
*/
pthread_cond_wait(&cond, &mtx);
}


p = head;
head = head->n_next;
printf("Got %d from front of queue\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 = 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("thread 1 wanna end the line.So cancel thread 2./n");

/*
* 关于pthread_cancel, 有一点额外的说明,
* 它是从外部终止子线程,子线程会在最近的取消点,退出线程;
* 而在咱们的代码里,最近的取消点确定就是pthread_cond_wait()了。
* 关于取消点的信息,有兴趣能够google,这里很少说了
*/
pthread_cancel(tid);
pthread_join(tid, NULL);

printf("All done -- exiting/n");
return 0;
}

 

转:http://blog.chinaunix.net/uid-26000296-id-3484910.html函数

相关文章
相关标签/搜索