【线程】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, ...
随机推荐
- 持续集成工具hudson
2008-07-08 一.什么是持续集成 持续集成的核心概念 CI 过程会经常构建软件组件:在许多情况下,每当源代码存储库(比如 Subversion 或 ClearCase)中的代码发生变化时, ...
- 第一次使用Mac
Mac禁用掉默认输入法 搜狗输入法已经包含中文.英文输入法了,其余输入法可以删除掉.按ctrol键的时候本来应该是搜狗输入法中英文切换,结果却是搜狗输入法和ABC输入法之间互相切换. 打开键盘偏好设置 ...
- java struts2入门学习--基于xml文件的声明式验证
一.知识点总结 后台验证有两种实现方式: 1 手工验证顺序:validateXxx(针对Action中某个业务方法验证)--> validate(针对Action中所有的业务方法验证) 2 声明 ...
- git学习笔记(二)—— 创建版本库&&版本管理
一.创建版本库 创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录: mkdir gitHub_CXWcd gitHub_CXW git init Initialized empty G ...
- Rust 之 cargo(项目构建和包管理工具)
如果食用cargo来进行项目构建: 1. 执行 cargo new hello_cargo --bin ,执行完上面的操作之后,我们切换到hell_cargo目录下,可以看到一个文件(Cargo.to ...
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist
1)创建试图时抛出此错误信息,如下图所示: 2)从网上搜索了一下,是SQL权限问题,通过如下的方式便可以解决: 3)再次执行创建视图的语句,验证一下问题是否已经解决,可以了,如下所示: 4)参考如下所 ...
- C语言中的 (void*)0 与 (void)0
前几天看到一个宏, 它大概是这样的: #define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LI ...
- Fix SharePoint 2013 Site in Read only mode after an interrupted backup
Problem When I was backing up SharePoint Site Collection Automatically with PowerShell and Windows T ...
- STM32定时器T2纯软件仿真时间准确,JTAG在线调试查看时间不准的问题
通过查看Sec的值和上次中断的差值计算的,虽然这个值是不准的 ,但实际上时间是准的, 原因如下:stm32在调试模式下虽然进断点之后程序停止了,但定时器的时钟还在走,计数器还在计数,若要在产生断点时计 ...
- JPA的多表复杂查询
转 JPA的多表复杂查询:详细篇 原文链接: https://mp.weixin.qq.com/s/7J6ANppuiZJccIVN-h0T3Q 2017-11-10 从小爱喝AD钙 最近工作中由于 ...