我有问题习惯了线程编程。我试图用互斥和锁构建一个构造。但我需要boost :: condition_variable :: wait两个锁作为参数。但是没有两个锁的等待功能。boost condition_variable等待多个锁
有没有人给我提示,或者我的方法完全错误?
我的目标是
- 允许同时添加(),全部写入缓冲区
- 工作者线程运算符()(),存储缓冲值
- 和搜索(),等待在搜索之前的工作线程
class Archive
{
vector externalBuffer;
vector swapBuffer;
vector internalBuffer;
boost::mutex externalBufferMutex;
boost::mutex allBufferMutex;
boost::condition_variable bufferIsFilledConditionVariable;
void operator()()
{
unique_lock allBufferLock(allBufferMutex);
while(true)
{
{
unique_lock lock(externalBufferMutex);
while(buffer.empty())
{
bufferIsFilledConditionVariable.wait(...); // I should call it with two locks?
}
externalBuffer.swap(swapBuffer);
}
{
internalBuffer.swap(swapBuffer);
// store values from internalBuffer
...
internalBuffer.clear();
}
}
}
void add(value) // pseudo code here
{
{
lock_guard lock(externalBufferMutex);
externalBuffer.push_back(value);
}
bufferIsFilledConditionVariable.notify_one();
}
// search will not be used concurrently with add()
// it should just wait for the worker thread
someReturnValue search()
{
unique_lock lock(allBufferMutex);
// search here
...
}
}
请详细说明你的问题:是什么内部缓冲区发生?它被搜查?何时和谁使用内部缓冲区中的数据?为什么你需要3个缓冲区?它提醒了古典生产者/消费者模式,除非我们没有看到消费者 – 2014-10-29 11:03:32