【线程】linux之多线程同步互斥技术
1.同步机制
2.技术示例
创建2个计数线程A和B,每次计数加1,当为偶数时,A线程计数;当为奇数时,B线程计数。
源码:
//thread_mutex_cond.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define MAX_COUNT9
pthread_mutex_t mutex;
pthread_cond_t cond;
int count=;
void AddCount_Odd_Func(void);
void AddCount_Even_Func(void);
int main()
{
int ret;
pthread_t odd_thread,even_thread;
pthread_attr_t thread_attr;
count = ;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
ret = pthread_attr_init(&thread_attr);
if (ret != )
{
perror("Attribute Creation Failed");
exit(EXIT_FAILURE);
}
pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED); ret=pthread_create(&odd_thread,&thread_attr,(void *)&AddCount_Odd_Func,NULL);
if(ret != )
{
perror("Thread Creation Failed");
exit(EXIT_FAILURE);
}
ret = pthread_create(&even_thread,&thread_attr,(void *)&AddCount_Even_Func, NULL);
if (ret != )
{
perror("Thread Creation Failed");
exit(EXIT_FAILURE);
}
while(count<MAX_COUNT);
printf("Finished!\n");
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
return ;
}
void AddCount_Odd_Func(void)
{
pthread_mutex_lock(&mutex);
while(count<MAX_COUNT)
{
if(count%==)
{
count++;
printf("AddCount_Odd_Func():count=%d.\n",count);
pthread_cond_signal(&cond);
}
else
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
}
void AddCount_Even_Func(void)
{
pthread_mutex_lock(&mutex);
while(count<MAX_COUNT)
{
if(count%==)
{
count++;
printf("AddCount_Even_Func():count=%d.\n",count);
pthread_cond_signal(&cond);
}
else
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
}
3.mystery注解
1)示例中,创建了互斥量mutex与条件量cond。mutex用于互斥操作,cond用于在相关条件成立时进行操作
2)pthread_attr_setdetachstate()函数设置线程为分离状态
3)pthread_cond_wait()使线程阻塞
4)pthread_cond_broadcast()函数用来唤醒所有被阻塞在条件变量cond上的线程。
要注意,被唤醒后的这些线程将再次竞争相应的互斥量
5)pthread_cond_init()函数创建条件变量
6)pthread_cond_signal()函数用来释放被阻塞在条件变量cond上的线程
【线程】linux之多线程同步互斥技术的更多相关文章
- 总结windows多线程同步互斥
windows多线程同步互斥--总结 我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同 ...
- windows多线程同步互斥--总结
我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同步--临界区 windows多线程同步 ...
- 【Linux】多线程同步的四种方式
背景问题:在特定的应用场景下,多线程不进行同步会造成什么问题? 通过多线程模拟多窗口售票为例: #include <iostream> #include<pthread.h> ...
- windows多线程同步--互斥量
关于互斥量的基本概念:百度百科互斥量 推荐参考博客:秒杀多线程第七篇 经典线程同步 互斥量Mutex 注意:互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...
- (转)Linux C 多线程编程----互斥锁与条件变量
转:http://blog.csdn.net/xing_hao/article/details/6626223 一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1. 初始化: 在 ...
- C++11 多线程同步 互斥锁 条件变量
在多线程程序中,线程同步(多个线程访问一个资源保证顺序)是一个非常重要的问题,Linux下常见的线程同步的方法有下面几种: 互斥锁 条件变量 信号量 这篇博客只介绍互斥量和条件变量的使用. 互斥锁和条 ...
- Python多线程同步互斥锁
接着上篇多线程继续讲,上篇最后的多线程共享全局变量对变量的处理值出错在本文中给出解决方案. 出现这个情况的原因是在python解释器中GIL全局解释器锁. GIL:全局解释器锁,每个线程在执行的过程都 ...
- linux 进程间同步互斥
参考链接: https://www.oschina.net/code/snippet_237505_8646 http://www.cnblogs.com/xilentz/archive/2012/1 ...
- linux C 多线程/线程池编程 同步实例
在多线程.线程池编程中经常会遇到同步的问题. 1.创建线程 函数原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...
随机推荐
- ubuntu中查看各种设备和资源的命令汇总
一.系统信息 1.查看内核信息: $uname -a 2.查看操作系统版本: ...
- linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解
一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...
- 进阶之路(基础篇) - 010 Arduino 函数(基本、串口、SPI)
一.基本函数 pinMode(引脚号,模式); digitalWrite(引脚号,电平状态); //默认低电平(或浮空) digitalRead(数字输入端口号); analogRe ...
- 【MySQL】MySQL之MySQL5.7安装包(msi文件)在Windows8下安装
最近自己在使用MySQL5.7.16.msi安装MySQL.自己下载的是.msi文件,在安装的过程中遇到了许多文件,网上大部分的Blog都是关于免安装包的安装方法,希望我的方法对大家有帮助. 1,下载 ...
- [MSP430] 对MSP430单片机__delay_cycles精确延时的说明及改正
在这里, 我来讨论一下关于MSP430单片机使用__delay_cycles延时的问题. IAR for MSP430编译器提供了一个编译器内联的精确延时函数(并非真正的 函数)以提供用户精确延时使用 ...
- python的内置下载器
python有个内置下载器,有时候在内部提供文件下载很好用. 进入提供下载的目录 # ls abc.aaa chpw.py finance.py lsdir.py ping.py u2d-partia ...
- mac 上运行cassandra出现的java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: : : unknown error错误解决方法
mac 上运行cassandra出现的java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostExce ...
- 利用 PowerShell 分析SharePoint WebApplication 体系结构
之前一篇文章<两张图看清SharePoint 2013 Farm 逻辑体系结构>谈到Web Application,Content Database,Site Collection的关系. ...
- 5.翻译:EF基础系列---EF中的上下文类
原文地址:http://www.entityframeworktutorial.net/basics/context-class-in-entity-framework.aspx EF中的上下文类是一 ...
- C#基础第二天-作业答案-九九乘法表-打印星星
题一:九九乘法表的答案 //正三角 ; i < ; i++) { ; j <= i; j++) { Console.Write("{0}*{1}={2} ", j, i ...