【发布时间】:2013-07-02 14:46:54
【问题描述】:
我的应用程序中目前有一个 boost::mutex 代码部分,看起来像这样
{//Lock
boost::unique_lock<boost::mutex> lock(some_mutex);
while(container.empty())
{
condition_var.wait(lock);
}/*Block if empty - for spurious wakeup*/
......
,,,,,,
}//Unlock
现在 some_mutex 是 boost::mutex 类型 并且 condition_var 的类型是 boost::condition_variable
现在 condition_var 是用condition_var.notifyone() 方法触发的,不幸的是,这个方法需要 boost::mutex 才能起作用。
我计划删除 boost::mutex 并使用提供的 CRITICAL_SECTION 窗口。但是我相信升压条件不适用于 windows CRITICAL_SECTION
对我的选项有什么建议可以用 CRITICAL_SECTION 替换 boost::mutex 并在上面的代码中进行最小的更改?
【问题讨论】:
-
您需要将 CRITICAL_SECTION 代码包装在提供锁定/解锁方法的类中,然后您可以使用
boost::condition_variable_any,它接受任何实现可锁定概念的类。 -
为什么要更换? boost::mutex 在功能上等同于 CRITICAL_SECTION。
-
我非常怀疑
CRITICAL_SECTION是否比boost::mutex快。查看 boost\thread\win32\basic_timed_mutex.hpp。这就是内联到您的代码中的内容。在非竞争互斥体的常见情况下,它基本上是一个内联的测试和设置。 CRITICAL_SECTION 执行过程调用。
标签: c++ multithreading locking critical-section