Condition Variables

Condition variables are synchronization primitives that enable threads to wait until a particular condition occurs.

Condition variables are user-mode objects that cannot be shared across processes.

Condition variables enable threads to atomically release a lock and enter the sleeping state.

They can be used with critical sections or slim reader/writer (SRW) locks.

Condition variables support operations that "wake one" or "wake all" waiting threads.

After a thread is woken, it re-acquires the lock it released when the thread entered the sleeping state.

条件变量是能够在特殊条件满足前使线程处于等待状态的同步原语.

条件变量是不能被跨进程共享的用户模式下的同步对象.

条件变量能够使线程原子性释放一个锁同一时候进入sleep 状态. 条件变量和Critical Section Object一起使用.

条件变量支持wake one 或者wake all 等待的线程.

Windows Server 2003 and Windows XP:  Condition variables are not supported.

It is often convenient to use more than one condition variable with the same lock.

For example, an implementation of a reader/writer lock might use a single critical section but separate condition variables for readers and writers.

在同一个锁上面使用条件变量是很实用的.

比方:对于同一个临界区通过把读操作和写操作通过条件变量来分离能够实现读锁,写锁以及读写锁.

The following code implements a producer/consumer queue.

The queue is represented as a bounded circular buffer, and is protected by a critical section.

The code uses two condition variables: one used by producers (BufferNotFull) and one used by consumers (BufferNotEmpty).

The code calls the InitializeConditionVariable function to create the condition variables.

The consumer threads call the SleepConditionVariableCS function to wait for items to be added to the queue and

the WakeConditionVariable function to signal the producer that it is ready for more items.

The producer threads call SleepConditionVariableCS to wait for the consumer to remove items from the queue and

 WakeConditionVariable to signal the consumer that there are more items in the queue.

实现一个生产者/消费者队列.

队列是一个被Critical Section Object 保护的有界限圆形BUFFER.

通过调用InitializeConditionVariable()函数去创建一个条件变量.

消费者调用SleepConditionVariableCS()函数等待有物品被增加到队列中,通过WakeConditionVariable()函数通知生产者生产很多其它的物品.

生产者调用SleepConditionVariableCS()函数等待消费者把物品从队列中移除,通过WakeConditionVariable()函数来通知消费者去消费很多其它的物品.

測试代码:UsingConditionVariables.cpp

#include <windows.h>
#include <stdlib.h>
#include <stdio.h> #define BUFFER_SIZE 10
#define PRODUCER_SLEEP_TIME_MS 500
#define CONSUMER_SLEEP_TIME_MS 2000 LONG Buffer[BUFFER_SIZE];
LONG LastItemProduced;
ULONG QueueSize;
ULONG QueueStartOffset; ULONG TotalItemsProduced;
ULONG TotalItemsConsumed; CONDITION_VARIABLE BufferNotEmpty;
CONDITION_VARIABLE BufferNotFull;
CRITICAL_SECTION BufferLock; BOOL StopRequested; DWORD WINAPI ProducerThreadProc (PVOID p)
{
ULONG ProducerId = (ULONG)(ULONG_PTR)p; while (true)
{
// Produce a new item. Sleep (rand() % PRODUCER_SLEEP_TIME_MS);
//原子锁
ULONG Item = InterlockedIncrement (&LastItemProduced);
//进入临界区,其它线程不能訪问下面被保护的资源
EnterCriticalSection (&BufferLock);
//仅仅有当有界缓冲区满了之后,才通知消费者来消费资源,否则就一直生产物品
while (QueueSize == BUFFER_SIZE && StopRequested == FALSE)
{
// Buffer is full - sleep so consumers can get items.
SleepConditionVariableCS (&BufferNotFull, &BufferLock, INFINITE);
} if (StopRequested == TRUE)
{
LeaveCriticalSection (&BufferLock);
break;
} // Insert the item at the end of the queue and increment size. Buffer[(QueueStartOffset + QueueSize) % BUFFER_SIZE] = Item;
QueueSize++;
TotalItemsProduced++; printf ("Producer %u: item %2d, queue size %2u\r\n", ProducerId, Item, QueueSize);
//离开临界区,其它线程可訪问该临界区
LeaveCriticalSection (&BufferLock); // If a consumer is waiting, wake it. WakeConditionVariable (&BufferNotEmpty);
} printf ("Producer %u exiting\r\n", ProducerId);
return 0;
}
//消费者线程
DWORD WINAPI ConsumerThreadProc (PVOID p)
{
//消费数量
ULONG ConsumerId = (ULONG)(ULONG_PTR)p; while (true)
{ //临界区,当一个线程在获取临界区权利时,其它线程都要等待.
EnterCriticalSection (&BufferLock);
//当前缓存区为零
while (QueueSize == 0 && StopRequested == FALSE)
{
// Buffer is empty - sleep so producers can create items.
//通知生产者进行生产物品.当生产者完毕生产后,则通知消费者来消费
SleepConditionVariableCS (&BufferNotEmpty, &BufferLock, INFINITE);
} if (StopRequested == TRUE && QueueSize == 0)
{
LeaveCriticalSection (&BufferLock);
break;
} // Consume the first available item. LONG Item = Buffer[QueueStartOffset]; QueueSize--;
QueueStartOffset++;
TotalItemsConsumed++; if (QueueStartOffset == BUFFER_SIZE)
{
QueueStartOffset = 0;
} printf ("Consumer %u: item %2d, queue size %2u\r\n",
ConsumerId, Item, QueueSize); LeaveCriticalSection (&BufferLock); // If a producer is waiting, wake it. WakeConditionVariable (&BufferNotFull); // Simulate processing of the item. Sleep (rand() % CONSUMER_SLEEP_TIME_MS);
} printf ("Consumer %u exiting\r\n", ConsumerId);
return 0;
} int main ( void )
{ InitializeConditionVariable (&BufferNotEmpty);
InitializeConditionVariable (&BufferNotFull); InitializeCriticalSection (&BufferLock); DWORD id;
HANDLE hProducer1 = CreateThread (NULL, 0, ProducerThreadProc, (PVOID)1, 0, &id);
HANDLE hConsumer1 = CreateThread (NULL, 0, ConsumerThreadProc, (PVOID)1, 0, &id);
HANDLE hConsumer2 = CreateThread (NULL, 0, ConsumerThreadProc, (PVOID)2, 0, &id); puts ("Press enter to stop...");
getchar(); EnterCriticalSection (&BufferLock);
StopRequested = TRUE;
LeaveCriticalSection (&BufferLock); WakeAllConditionVariable (&BufferNotFull);
WakeAllConditionVariable (&BufferNotEmpty); WaitForSingleObject (hProducer1, INFINITE);
WaitForSingleObject (hConsumer1, INFINITE);
WaitForSingleObject (hConsumer2, INFINITE); printf ("TotalItemsProduced: %u, TotalItemsConsumed: %u\r\n",
TotalItemsProduced, TotalItemsConsumed);
return 0;
}

Condition Variables的更多相关文章

  1. 使用Condition Variables 实现一个线程安全队列

    使用Condition Variables实现一个线程安全队列 测试机: i7-4800MQ .7GHz, logical core, physical core, 8G memory, 256GB ...

  2. 深入解析条件变量(condition variables)

    深入解析条件变量 什么是条件变量(condition variables) 引用APUE中的一句话: Condition variables are another synchronization m ...

  3. 并行编程条件变量(posix condition variables)

    在整理Java LockSupport.park()东方的,我看到了"Spurious wakeup",通过重新梳理. 首先,可以在<UNIX级别编程环境>在样本: # ...

  4. [development][C] 条件变量(condition variables)的应用场景是什么

    产生这个问题的起因是这样的: ‎[:] ‎<‎tong‎>‎ lilydjwg: 主线程要启动N个子线程, 一个局部变量作为把同样的参数传入每一个子线程. 子线程在开始的十行会处理完参数. ...

  5. 4.锁--并行编程之条件变量(posix condition variables)

    在整理Java LockSupport.park()的东东.看到了个"Spurious wakeup".又一次梳理下. 首先来个<UNIX环境高级编程>里的样例: [c ...

  6. c++11多线程记录6:条件变量(condition variables)

    https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...

  7. 第8章 用户模式下的线程同步(4)_条件变量(Condition Variable)

    8.6 条件变量(Condition Variables)——可利用临界区或SRWLock锁来实现 8.6.1 条件变量的使用 (1)条件变量机制就是为了简化 “生产者-消费者”问题而设计的一种线程同 ...

  8. java线程并发控制:ReentrantLock Condition使用详解

    本文摘自:http://outofmemory.cn/java/java.util.concurrent/lock-reentrantlock-condition java的java.util.con ...

  9. android分析之Condition

    Condition的含义是条件变量,其实现依赖于系统,一般都要配合Mutex使用,使用步骤为:给mutex上锁(Lock),调用wait等待"条件"发生,如果没有发生则re-wai ...

随机推荐

  1. AndroidManifest.xml文件解析(转帖)

    原帖地址:http://www.cnblogs.com/pilang/archive/2011/04/20/2022932.html 一.关于AndroidManifest.xml       And ...

  2. 面试题-Redis、MongoDB、Memcached[转]

    https://blog.csdn.net/gangsijay888/article/details/81213811 一.缓存 搞懂缓存那些事:https://blog.csdn.net/a7248 ...

  3. 关于微软C#中的CHART图表控件的简单使用【转】

    最近公司项目要用到Chart图表控件,这是一个比较老的东西了,目前网络上似乎已经不太流行这个控件,但是只要配置了相关的属性,效果还是可以的.前前后后摸索了好久,接下来谈谈这个件控件最重要的几个属性. ...

  4. 使用C++/libCurl/Jsoncpp读取arcgis wmts 服务(restful模式)

    前言: 最近工作需要将arcgis的wmts服务接入我们的3DGis系统平台,要求用户只输入一个rest模式的wmts服务地址,系统即可自动获取并解析其元数据信息,生成wmts图层,并渲染显示.经过多 ...

  5. 如何在Windows版的ScaleIO的节点中添加磁盘

    嗯, 为什么会有这个问题呢? 因为我要安装ScaleIO 1.32, 在使用Installation manager完成初始安装之后, 需要将一些磁盘添加到ScaleIO的storage pool中. ...

  6. Base64原理与实现

    Base64编码说明 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节,则用0填充 ...

  7. 【Spark】SparkStreaming-Kafka-集成-终极参考资料

    SparkStreaming-Kafka-集成-终极参考资料 Spark Streaming和Kafka整合开发指南(二) – 过往记忆 Streamingkafka零丢失 | 等英博客 spark- ...

  8. Linux解决安装包无法找到问题Unable to locate package rar

    1:问题 在默认安装的Linux中有时候会找不到需要安装的软件包,那就需要更新了 例如在执行apt-get install xx的时候就报错了 E: Unable to locate package ...

  9. 如何拷贝一个wchar_t类型的字符串

    Do this, wchar_t clone[260]; wcscpy(clone,szPath); Or, if you want to allocate memory yourself, wcha ...

  10. Maven hive-jdbc教程

    由于项目中需要用到hive-jdbc从数据仓库拉数据下来,所以简单的学一下hive,hive数据仓库建构在hadoop集群之上,数据存在hdfs文件系统中,hive中执行的操作会装换成mapreduc ...