LinuxC多线程编程第五篇:线程通信(Condition)
来源:互联网 发布:端口号 传输层 编辑:程序博客网 时间:2024/06/07 02:55
线程同步还有一种情况,线程1需要等某个条件成立才能继续往下执行,如果这个条件不成立,线程1就阻塞等待,线程2在执行某个条件成立了就唤醒线程1。这个和Java中的wait()和notify()其实是一样的
注意:最下面有本文演示的源码的链接地址
初始化与销毁通信条件
#include <pthread.h>
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
以上和上一篇中Mutex锁比较类似,我就不再重复。
具体实现函数
#include <pthread.h>
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
这几个函数更加函数名就可以理解它的意思了,我就不多说。pthread_cond_signal、pthread_cond_broadcast与Java中notify、notifyAll()也是一样的。
pthread_cond_wait在一个conditon上阻塞等待,这个函数要做3个步骤:
- 释放Mutex
- 阻塞等待
- 当其它线程调用pthread_cond_signal或pthread_cond_signal,它会重写获取锁。
我简单的写了一个阻塞栈,方便大家理解:
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- struct node{
- struct node* next;
- int number;
- };
- pthread_cond_t hasNode = PTHREAD_COND_INITIALIZER;
- pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
- struct node* head = NULL;
- void* get(void *arg)
- {
- struct node *node;
- while(1)
- {
- pthread_mutex_lock(&lock);
- if(head == NULL)
- {
- printf("has no data...\n");
- pthread_cond_wait(&hasNode,&lock);
- }
- node = head;
- head = node->next;
- printf("get the head node number:%d\n",node->number);
- pthread_mutex_unlock(&lock);
- free(node); //移除获取到的节点
- sleep(rand()%5);
- }
- }
- void* put(void *p)
- {
- struct node* node;
- while(1)
- {
- node = malloc(sizeof(struct node));
- node->number = rand()%1000 +1;
- printf("put the head node number:%d\n",node->number);
- pthread_mutex_lock(&lock);
- node->next = head;
- head = node;
- pthread_mutex_unlock(&lock);
- pthread_cond_signal(&hasNode);
- sleep(rand()%5);
- }
- }
- int main(){
- pthread_t pid,cid;
- srand(time(NULL));
- pthread_create(&pid,NULL,get,NULL);
- pthread_create(&cid,NULL,put,NULL);
- pthread_join(pid,NULL);
- pthread_join(cid,NULL);
- return 0;
- }
编译运行结果如下
源码下载:http://download.csdn.net/detail/jefry_xdz/4246164
- LinuxC多线程编程第五篇:线程通信(Condition)
- LinuxC多线程编程第五篇:线程通信(Condition)
- LinuxC多线程编程第二篇:线程终止
- LinuxC多线程编程第二篇:线程终止
- 多线程编程入门(14):Condition实现线程通信
- LinuxC多线程编程第四篇:线程同步mutex
- LinuxC多线程编程第四篇:线程同步mutex
- LinuxC多线程编程第一篇:线程创建
- LinuxC多线程编程第一篇:线程创建
- JAVA5多线程---Condition使用---线程通信
- JAVA5多线程---Condition使用---线程通信
- JAVA多线程---Condition使用---线程通信
- JAVA5多线程---Condition使用---线程通信
- 多线程之线程通信条件Condition
- 多线程之线程通信条件Condition二
- 【多线程】多线程编程:线程通信
- 多线程Demo-Condition通信
- 多线程之8 -------Condition 实现线程同步通信
- Oracle Linux 4.7下配置udev
- getaddrinfo函数的说明
- LinuxC多线程编程第三篇:pthread_join与phread_datach介绍
- 利用SSH框架实现用户登录功能
- LinuxC多线程编程第四篇:线程同步mutex
- LinuxC多线程编程第五篇:线程通信(Condition)
- Python函数式编程指南(四):生成器
- python学习1:字符串的使用
- 解决office 2013在win 8.1 输入法超级慢
- POJ 1062:昂贵的聘礼:简单的dij最短路变形
- Unique Path
- STM32 定时器中断
- 数学之美系列二十一:布隆过滤器(Bloom Filter)
- 一个引起service为空的原因