【线程】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, ...
随机推荐
- 递归查询构造jquery tree
1 现在有如下的一张表: CREATE TABLE [dbo].[ThemeCategory] ( [ID] [int] NOT NULL, [ThemeCategoryName] [nvarchar ...
- iOS 10 的一个重要更新-线程竞态检测工具 Thread Sanitizer
本文介绍了 Xcode 8 的新出的多线程调试工具 Thread Sanitizer,可以在 app 运行时发现线程竞态. 想想一下,你的 app 已经近乎大功告成:它经过精良的打磨,单元测试全覆盖. ...
- 【LeetCode】210. Course Schedule II
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...
- JAVA删除文件及文件夹
JAVA在删除文件或文件夹时,在java.io.File类下有个delete的方法,并且可以返回true or false, 用这个方法来删除单个文件时,很好使,但在删除文件夹时,如果文件夹下面有文件 ...
- PHP通过soap调用c#的WebService
1:C# Contact public class Contact { private int _Id; private string _Name; public int id { get { ret ...
- mybatis自定义插件动态修改sql语句
step1:定义Interceptor实现org.apache.ibatis.plugin.Interceptor import org.apache.commons.logging.Log; imp ...
- Android Studio 2.3 正式版新功能,你不来看看?!
2017.3.3 Google老大发布了Android Studio 2.3正式版. 在许多2.3beta版本的基础上修复了bug然后推出了正式版.提供了一些新特性,和对部分已有功能的修改完善. Bu ...
- solr开发,提交索引数据的几种方式
今天抽空学习了一下solr,有新东西学习就是哈皮! 期待能有机会与实战.实例仅为个人理解学习实例.提交到Solr服务器上的数据必须是 SolrInputDocument 类型. 方案一:利用反射,自定 ...
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区别
request.getRequestURI() /jqueryWeb/resources/request.jsprequest.getRequestURL() http://localhost:808 ...
- Eclipse SQLExplorer插件的安装和使用
from: http://blog.csdn.net/flashlm/archive/2007/06/30/1672836.aspx 插件名称: SQLExplorer 插件分类: SQL Edito ...