说明:对多线程与相互排斥锁不熟悉的请參考其他

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化相互排斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
void *thread1(void *);
void *thread2(void *);
int i=1;
int main(void)
{
pthread_t t_a;
pthread_t t_b;
pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
pthread_join(t_a, NULL);/*等待进程t_a结束 和windows的waitForSingleObject 相似*/
pthread_join(t_b, NULL);/*等待进程t_b结束 和windows的waitForSingleObject 相似*/
/* 释放资源*/
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit(0);
}
void *thread1(void *junk)
{
for(i=1;i<=6;i++)
{
printf("now i = %d\n", i);
pthread_mutex_lock(&mutex);/*锁住相互排斥量*/
printf("thread1: lock %d\n", __LINE__);
if(i%3==0){
printf("thread1:signal 1 %d\n", __LINE__);
pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
printf("thread1:signal 2 %d\n", __LINE__);
sleep(1);
}
pthread_mutex_unlock(&mutex);/*解锁相互排斥量*/
printf("thread1: unlock %d\n\n", __LINE__);
sleep(1);
}
}
void *thread2(void *junk)
{
while(i<6)
{
pthread_mutex_lock(&mutex);
printf("thread2: lock %d\n", __LINE__);
if(i%3!=0){
printf("thread2: wait 1 %d\n", __LINE__);
/*pthread_cond_wait 函数将解锁mutex。并使当前线程堵塞在cond指向的条件变量上*/
pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/
printf("thread2: wait 2 %d\n", __LINE__);;
}
pthread_mutex_unlock(&mutex);
printf("thread2: unlock %d\n\n", __LINE__);
sleep(1);
}
}

root@ubuntu:/usr/syw/linux/Thread# gcc -g threadDemo3_arg.c -o thread3
-lpthread

root@ubuntu:/usr/syw/linux/Thread# ./thread3

now i = 1

thread2: lock 44

thread2: wait 1  46

thread1: lock 27

thread1: unlock 35

now i = 2

thread1: lock 27

thread1: unlock 35

now i = 3

thread1: lock 27

thread1:signal 1  29

thread1:signal 2  31

thread1: unlock 35

thread2: wait 2  49

thread2: unlock 52

now i = 4

thread2: lock 44

thread2: wait 1  46

thread1: lock 27

thread1: unlock 35

now i = 5

thread1: lock 27

thread1: unlock 35

now i = 6

thread1: lock 27

thread1:signal 1  29

thread1:signal 2  31

thread1: unlock 35

thread2: wait 2  49

thread2: unlock 52

最新文章

  1. MVVM页面跳转 技巧性标记
  2. codevs 1015 计算器的改良 2000年NOIP全国联赛普及组
  3. lua 快速排序
  4. MySQL注入
  5. PHP连接SQLServer
  6. 如何实现GridView的选中,编辑,取消,删除功能
  7. [p2p]UDP用打洞技术穿透NAT的原理与实现
  8. spring-qualifier解释
  9. openwrt之snmpd
  10. 一天工作所用到的Git命令
  11. Algorithm --&gt; 全排列
  12. JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建
  13. 服务器对接码云webhooks
  14. Ubuntu修改Apache默认Web端口
  15. Node、PHP、Java 和 Go 服务端 I/O 性能PK
  16. 【Python】JBOSS-JMX-EJB-InvokerServlet批量检测工具
  17. test4
  18. PHP重载以及Laravel门面Facade
  19. 数据库如何从SQL server转换到SQLite
  20. TCL函数“参数自动补全” 与 “help 信息显示”

热门文章

  1. nginx限速白名单配置
  2. 【bzoj2044】三维导弹拦截 dp+二分图最大匹配
  3. HITOJ 2739 The Chinese Postman Problem(欧拉回路+最小费用流)
  4. 【BestCoder #48】
  5. [NOWCODER] myh的超级多项式
  6. [SDOI2015][bzoj3990] 序列 [搜索]
  7. Codeforces 835 F Roads in the Kingdom(树形dp)
  8. busybox根文件系统使用记录
  9. Educational Codeforces Round 42 (Rated for Div. 2) C
  10. Heine-Borel定理