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之多线程同步互斥技术的更多相关文章

  1. 总结windows多线程同步互斥

    windows多线程同步互斥--总结 我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同 ...

  2. windows多线程同步互斥--总结

    我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同步--临界区 windows多线程同步 ...

  3. 【Linux】多线程同步的四种方式

    背景问题:在特定的应用场景下,多线程不进行同步会造成什么问题? 通过多线程模拟多窗口售票为例: #include <iostream> #include<pthread.h> ...

  4. windows多线程同步--互斥量

    关于互斥量的基本概念:百度百科互斥量 推荐参考博客:秒杀多线程第七篇 经典线程同步 互斥量Mutex 注意:互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...

  5. (转)Linux C 多线程编程----互斥锁与条件变量

    转:http://blog.csdn.net/xing_hao/article/details/6626223 一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1. 初始化: 在 ...

  6. C++11 多线程同步 互斥锁 条件变量

    在多线程程序中,线程同步(多个线程访问一个资源保证顺序)是一个非常重要的问题,Linux下常见的线程同步的方法有下面几种: 互斥锁 条件变量 信号量 这篇博客只介绍互斥量和条件变量的使用. 互斥锁和条 ...

  7. Python多线程同步互斥锁

    接着上篇多线程继续讲,上篇最后的多线程共享全局变量对变量的处理值出错在本文中给出解决方案. 出现这个情况的原因是在python解释器中GIL全局解释器锁. GIL:全局解释器锁,每个线程在执行的过程都 ...

  8. linux 进程间同步互斥

    参考链接: https://www.oschina.net/code/snippet_237505_8646 http://www.cnblogs.com/xilentz/archive/2012/1 ...

  9. linux C 多线程/线程池编程 同步实例

    在多线程.线程池编程中经常会遇到同步的问题. 1.创建线程 函数原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...

随机推荐

  1. <转>Boost库之asio io_service以及run、run_one、poll、poll_one区别

    本文转自:http://blog.csdn.net/byxdaz/article/details/71088812 一.io_service的作用 io_servie 实现了一个任务队列,这里的任务就 ...

  2. leetcode44:wildcard

    44. Wildcard Matching 问题描述 给定字符串s和模式p,判断字符串s是否完全符合模式p 其中字符串s只包含小写字母,模式串p包含小写字母.*.?,其中星号表示任意长度的任意字符串, ...

  3. Delphi单元文件引用名称问题

    Delphi新版本的单元文件格式变化了,如windows变成了winapi.windows,如果想在单元引用中使用简称,则需要在工程选项中配置: 这样就可以使用全名或简写来引用单元了.

  4. Xcode 8 的 Debug 新特性 —- WWDC 2016 Session 410 & 412 学习笔记

    Contents OverView Static Analyzer Localizability Instance Cleanup Nullablility Runtime Issue View De ...

  5. 【LeetCode】200. Number of Islands (2 solutions)

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  6. React(0.13) 定义一个多选的组件

    <!DOCTYPE html> <html> <head> <title>React JS</title> <script src=& ...

  7. C# 用timer做成服务后 timer_Tick () 为什么不执行?

    不能使用 窗体的 Timer,他只能在窗体中使用,服务中无法使用请使用 System.Timers.Timer类 protected override void OnStart(string[] ar ...

  8. codesmith连接postgresql修复bug

    转:CodeSmith7代码生成器针对PostgreSQL数据库无法使用的Bug修复全过程 我自己又修改过,完整的PostgreSQLSchemaProvider.cs文件如下 using Npgsq ...

  9. 解决方案,org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    org.hibernate.LazyInitializationException: could not initialize proxy - no Session org.hibernate.pro ...

  10. 下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法

    下载远程(第三方服务器)文件.图片,保存到本地(服务器)的方法.保存抓取远程文件.图片   将一台服务器的文件.图片,保存(下载)到另外一台服务器进行保存的方法: 1 #region 图片下载 2 3 ...