有关pthread锁和条件同步
来源:互联网 发布:电脑网络ip地址查询 编辑:程序博客网 时间:2024/06/14 15:37
代码:
#include <stdio.h>#include <stdlib.h>#include <getopt.h> //getopt_long()头文件位置 #include <string.h>#include <unistd.h>#include <syslog.h>#include <pthread.h>void *thread1_function(void *arg);void *thread2_function(void *arg);pthread_mutex_t mutex;pthread_cond_t cond;int g_value = 0;int main(int argc, char *argv[]){ int rc1,rc2; char *str1="wenhaoll"; char *str2="linglong"; pthread_t thread1,thread2; pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond,NULL); if((rc1 = pthread_create(&thread1,NULL,thread1_function,str1))) { fprintf(stdout,"thread 1 create failed: %d\n",rc1); } if((rc2 = pthread_create(&thread2,NULL,thread2_function,str2))) { fprintf(stdout,"thread 2 create failed: %d\n",rc2); } pthread_join(thread1,NULL); pthread_join(thread2,NULL); return 0;}void *thread1_function(void *arg){ pthread_cond_wait(&cond, &mutex); char i; for (i=0; i<9; i++) { pthread_mutex_lock(&mutex); g_value ++; pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); sleep(2); } return NULL;}void *thread2_function(void *arg){ char i; for (i=0; i<9; i++) { pthread_mutex_lock(&mutex); pthread_cond_wait(&cond, &mutex); printf("g_value is: %d\n", g_value); pthread_mutex_unlock(&mutex); } return NULL;}
0 0
- 有关pthread锁和条件同步
- 线程和线程同步(pthread)
- 线程和线程同步(pthread)
- pthread几个互斥锁和条件变量接口
- Pthread 线程 —— 多线程同步 条件变量(cond)
- pthread同步
- 多线程和同步01-pthread库
- 同步和条件变量
- pthread-生产者消费者问题【采用互斥量和条件变量】
- 【pthread系列-6】条件变量基本概念和原理
- pthread和std::thread中条件变量的使用
- PTHREAD编程--条件变量
- pthread 条件变量
- pthread线程和线程锁
- 使用pthread互斥体+条件变量实现读写锁
- pthread条件变量condition(配合mutex锁使用)
- Golang同步:条件变量和锁组合使用
- 线程同步之互斥锁、读写锁和条件变量
- 有一个二叉树, 节点全部为整数,如何找到一个子树,它所有节点的和最大?要求编程序实现。
- DHCP报文种类分析 -- l路由linux
- iOS 获取当前时间并且显示走在视图上
- hbase shell
- Android 实现个性的ViewPager切换动画 实战PageTransformer
- 有关pthread锁和条件同步
- 阿里巴巴集团2014校园招聘笔试题(9月22北京)
- 直接插入排序基本思想
- 【BZOJ1095】【ZJOI2007】Hide 捉迷藏 线段树维护括号序列 数据结构的压缩。
- php json_decode null
- JavaScript 项目构建工具 Grunt的安装
- 高效多边形填充算法及其C例程
- android toleftof torightof注意事项
- 九度oj-1388-跳台阶