增强型静音和条件变量
Boost Interprocess mutexes and condition variables
我正在查看此启动示例代码的两个过程,它们之间共享一个sutex和条件变量:
https://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchronization_mechanisms.html
,但我不明白这里的静音条件变量设计如何工作。
初始过程调用:
for(int i = 0; i < NumMsg; ++i){
scoped_lock<interprocess_mutex> lock(data->mutex); // Take mutex
if(data->message_in){
data->cond_full.wait(lock); // Wait
}
if(i == (NumMsg-1))
std::sprintf(data->items, "%s", "last message");
else
std::sprintf(data->items, "%s_%d", "my_trace", i);
//Notify to the other process that there is a message
data->cond_empty.notify_one(); // Notify
//Mark message buffer as full
data->message_in = true;
}
和第二个过程调用:
bool end_loop = false;
do{
scoped_lock<interprocess_mutex> lock(data->mutex); // Take mutex
if(!data->message_in){
data->cond_empty.wait(lock); // Wait
}
if(std::strcmp(data->items, "last message") == 0){
end_loop = true;
}
else{
//Print the message
std::cout << data->items << std::endl;
//Notify the other process that the buffer is empty
data->message_in = false;
data->cond_full.notify_one(); // Notify
}
}
while(!end_loop);
要致电wait()
或notify()
任何一个过程都必须保持共享的静音,因此,如果一个过程在wait()
上,则肯定无法调用notify()
?
等待在等待时释放哑光,因此另一个线程可以获取sutex并执行通知。
相关文章:
- 基于模板值的条件变量
- 没有超时的C++条件变量
- 在条件变量中触发错误信号的频率是多少
- 在通知提升间处理条件变量时未按住锁会导致问题
- 通知条件变量后使用互斥锁
- 滥用条件变量
- 升压插值条件变量可以虚假唤醒吗?
- 子线程中的条件变量等待停止主线程中的执行
- 条件变量基本示例
- 正在连接的等待条件变量的线程会发生什么情况?
- C++11如何在1个线程中使用条件变量处理2个线程安全队列
- 当线程处理不同的类时,应该在哪里声明条件变量、互斥对象
- 为什么在同一条件变量上使用多个互斥锁会使此代码崩溃?
- 条件变量:wait_for.gcc错误
- 如何"stop"正在等待条件变量的分离线程?
- 如何杀死被条件变量锁定的线程?
- 使用互斥锁和条件变量作为成员时如何修复"use of deleted function"?
- C++ 多个使用者线程卡在条件变量上
- POSIX 条件变量和互斥体"竞争"
- 将cpp_redis pub/sub与条件变量一起使用时出现问题