#include <pthread.h>
int thread_flag;
pthread_cond_t thread_flag_cv;
pthread_mutex_t thread_flag_mutex;
void initialize_flag ()
{
/* Initialize the mutex and condition variable.
pthread_mutex_init (&thread_flag_mutex, NULL);
pthread_cond_init (&thread_flag_cv, NULL);
/* Initialize the flag value. */
thread_flag = ;
}
*/
/* Calls do_work repeatedly while the thread flag is set; blocks if
the flag is clear. */
void* thread_function (void* thread_arg)
{
/* Loop infinitely. */
while () {
/* Lock the mutex before accessing the flag value. */
pthread_mutex_lock (&thread_flag_mutex);
while (!thread_flag)
/* The flag is clear. Wait for a signal on the condition
variable, indicating that the flag value has changed. When the
signal arrives and this thread unblocks, loop and check the
flag again. */
pthread_cond_wait (&thread_flag_cv, &thread_flag_mutex);
/* When we’ve gotten here, we know the flag must be set. Unlock
the mutex. */
pthread_mutex_unlock (&thread_flag_mutex);
/* Do some work. */
do_work ();
}
return NULL;
}
/* Sets the value of the thread flag to FLAG_VALUE.
*/
void set_thread_flag (int flag_value)
{
/* Lock the mutex before accessing the flag value. */
pthread_mutex_lock (&thread_flag_mutex);
/* Set the flag value, and then signal in case thread_function is
blocked, waiting for the flag to become set. However,
thread_function can’t actually check the flag until the mutex is
unlocked. */
thread_flag = flag_value;
pthread_cond_signal (&thread_flag_cv);
/* Unlock the mutex. */
pthread_mutex_unlock (&thread_flag_mutex);
}

multi-threads synchronization use conditional mutex的更多相关文章

  1. Python threads synchronization: Locks, RLocks, Semaphores, Conditions, Events and Queues(Forwarding)

    This article describes the Python threading synchronization mechanisms in details. We are going to s ...

  2. A Basic Example of Threads Synchronization in Python, python中的线程同步示例

    http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/ We will ...

  3. Thread in depth 3:Synchronization

    Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...

  4. Uniform synchronization between multiple kernels running on single computer systems

    The present invention allocates resources in a multi-operating system computing system, thereby avoi ...

  5. c+11 std::condition_variable and mutex

    multiple threads synchronization primitive: 多线程同步语义 多线程的同步语义是多线程编程的核心,线程之间通过同步语义进行通信,实现并发.C++ JAVA 中 ...

  6. linux 条件变量与线程池

    条件变量Condition Variables 概述 1. 条件变量提供了另外一种线程同步的方式.如果没有条件变量,程序需要使用线程连续轮询(可能在临界区critical section内)方式检查条 ...

  7. C++11的简单线程池代码阅读

    这是一个简单的C++11实现的线程池,代码很简单. 原理就是管理一个任务队列和一个工作线程队列. 工作线程不断的从任务队列取任务,然后执行.如果没有任务就等待新任务的到来.添加新任务的时候先添加到任务 ...

  8. sysbench的安装与使用(with MySQL)

    sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试. 项目主页: http://sysbench.sourceforge.net/ 安装文档htt ...

  9. sysbench压力测试工具简介和使用(一)

    sysbench压力测试工具安装和参数介绍 一.sysbench压力测试工具简介: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据 ...

随机推荐

  1. HDU 1011-Starship Troopers(树形背包)

    题意: 有n个洞,连接像一棵树,每个包含一定数量的怪和价值,给你m个士兵,每个士兵能打20个怪,杀完一个洞的怪可得该洞的价值才可继续打相连的下面的洞(每个士兵只能打一个洞),求获得的最大价值. 分析: ...

  2. PC端使用opencv获取webcam,通过socket把Mat图像传输到android手机端

    demo效果图: PC端 android端 大体流程 android端是服务器端,绑定IP和端口,监听来自PC端的连接, pc端通过socket与服务器andorid端传输图片. 主要代码 andro ...

  3. vi常用命令与设置(不断修改中)

    注:前缀':'表示vim命令(命令模式),前缀'$'表示为shell命令,表示Ctrl + W,表示Ctrl + Shift+ 6,无前缀者均为普通模式下的键盘按键.基本的插入与修改:         ...

  4. Guide to make CentOS 7 the perfect desktop

    原文地址:  http://www.dedoimedo.com/computers/fedora-pimp.html My original review of CentOS 7 was less e ...

  5. Android实例-退出程序(XE8+小米2)

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  6. Javascript注意事项一【防止浮点数溢出】

    num = 0.1+0.2; //0.30000000000000004 a = (1+2)/10; //0.3(浮点数中的整数运算时精确的)

  7. NGUI学习笔记(一):官方视频学习记录

    学习NGUI一直断断续续的,目前打算做一个总结的笔记. 我使用的是比较老的3.6.0版本. 1.使用NGUI,需要开启“Edit”->“Project Settings”->“Physic ...

  8. iOS 限制textField输入的长度

    1.电话号码(带3-3-4效果) //指定代理 self.phoneTextField.delegate = self; //当编辑改变的时候,进行字符校验 [self.phoneTextField ...

  9. iOS 键盘框架IQKeyboardManager使用

    框架地址:https://github.com/hackiftekhar/IQKeyboardManager AppDelegate.m文件中   #import <IQKeyboardMana ...

  10. 【转】SQL语句中的正则表达示

    正则表达式作用是匹配方本,将一个模式(正则表达式)与一个文本串进行比较. MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. MySQL仅支持 ...