condition variable 和 srwlock的一个例子
来源:互联网 发布:ubuntu如何卸载jdk 编辑:程序博客网 时间:2024/05/23 10:27
Condition Variable
为实现近点的生产者消费者问题。我们可以使用两个CONDITION_VARIABLE:g_full,g_empty来实现。在缓冲区满的时候,生产者线程调用SleepConditionVariableSRW(&g_full, &g_lock, INFINITE, 0)施放获得的锁并等待g_full。缓冲区空的时候,消费者可以调用leepConditionVariableSRW(&g_empty, &g_lock, INFINITE, 0)施放获得的锁并等待g_empty。掉进满足后,可是使用WakeAllConditionVariable唤醒所有等待的线程或者使用WakeConditionVariable唤醒一个等待的线程。
和Condition Variable配置使用的可以使CrticalSection也可以使SRWLock。
BOOL SleepConditionVariableCS(
PCONDITION_VARIABLE pConditionVariable,
PCRITICAL_SECTION pCriticalSection,
DWORD dwMilliseconds);
BOOL SleepConditionVariableSRW(
PCONDITION_VARIABLE pConditionVariable,
PSRWLOCK pSRWLock,
DWORD dwMilliseconds,
ULONG Flags);
参数dwMilliseconds指定等待超时的时间,如果超时方法返回FASLE;INFINITE指定等待不超时。参数Flags指定被唤醒时尝试获得的锁的类型。CONDITION_VARIABLE_LOCKMODE_ SHARED指定获得共享锁或者0指定获得独占锁。
const int MAX_SIZE = 10;
CONDITION_VARIABLE g_full;
CONDITION_VARIABLE g_empty;
SRWLOCK g_lock;
list<Product> products;
unsigned int WINAPI ProduceThreadFunc(void* pvParam)
{
int i(0);
while(true)
{
Sleep(rand() % 100);
AcquireSRWLockExclusive(&g_lock);
if (products.size() >= MAX_SIZE)
{
SleepConditionVariableSRW(&g_full, &g_lock, INFINITE, 0);
}
else
{
cout <<"Produce Product:" <<i <<" by thread " <<GetThreadId(GetCurrentThread()) <<endl;
products.push_back(Product(i++));
}
WakeAllConditionVariable(&g_empty);
ReleaseSRWLockExclusive(&g_lock);
}
return 0;
}
unsigned int WINAPI ConsumeThreadFunc(void* pvParam)
{
while(true)
{
Sleep(rand() % 100);
AcquireSRWLockExclusive(&g_lock);
if(products.size() == 0)
{
SleepConditionVariableSRW(&g_empty, &g_lock, INFINITE, 0);
}
else
{
Product p = products.front();
products.pop_front();
cout <<"Consume Product:" <<p.m_no <<" by thread " <<GetThreadId(GetCurrentThread()) <<endl;
}
WakeAllConditionVariable(&g_full);
ReleaseSRWLockExclusive(&g_lock);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned)time(NULL));
InitializeSRWLock(&g_lock);
unsigned int threadId;
HANDLE thread1 = (HANDLE)_beginthreadex(NULL, 0, ProduceThreadFunc, NULL, 0, &threadId);
HANDLE thread2 = (HANDLE)_beginthreadex(NULL, 0, ConsumeThreadFunc, NULL, 0, &threadId);
HANDLE thread3 = (HANDLE)_beginthreadex(NULL, 0, ConsumeThreadFunc, NULL, 0, &threadId);
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
WaitForSingleObject(thread3, INFINITE);
system("pause");
return 0;
}
- condition variable 和 srwlock的一个例子
- condition variable 和 srwlock的一个例子
- 自己写的CONDITION_VARIABLE和SRWLock配合使用的例子
- 关于Condition Variable为什么需要一个Mutex的思考
- srwlock和条件变量例子2
- mutex 和 condition variable[转载]
- 关于condition variable的理解
- Condition 很好的例子
- 对条件变量(condition variable)的讨论
- 对条件变量(condition variable)的讨论
- 对条件变量(condition variable)的讨论
- 对条件变量(condition variable)的讨论
- 线程同步的方式——Mutex(互斥量)、 Condition variable(条件变量)和Semaphore(信号量)
- condition variable in os161
- Condition variable 2
- Discussion about Condition Variable
- Condition Variable Spurious Wakes
- 条件变量 condition variable
- sql中实现split()功能
- 数据结构实验之串一:KMP简单应用
- 表格合并---课程表
- Android源码工程转化为Android Studio项目的方法
- solr5.X的几个重要配置模板
- condition variable 和 srwlock的一个例子
- 假设检验介绍
- 盒模型再回顾:外边距折叠与怪异盒模型
- C++空类编译器自动生成的6个成员函数
- Android 点击按钮实现控件显示隐藏
- 用Python在训练好的log文件中提取出数据并画图
- 001
- [CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
- 前台如何调用MD5加密