boost :: interprocess_mutex vs Win32 native mutexes的性能是什么? [英] What is the performance of boost::interprocess_mutex vs Win32 native mutexes?

查看:286
本文介绍了boost :: interprocess_mutex vs Win32 native mutexes的性能是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我可以在boost源代码中进行研究,如果没有任何人有答案,可以这样做来回答我自己的好奇心。

Note that I can conduct the research inside the boost source code, and may do this to answer my own curiosity if there isn't anyone out there with an answer.

我确实要问,因为也许有人已经做了这个比较,并可以权威回答?

I do ask however because maybe someone has already done this comparison and can answer authoritatively?

似乎在流程之间创建一个共享内存映射文件,与 InterlockedIncrement()可以创建一个类似于 CRITICAL_SECTION 的大量的用户模式互斥, Win32 Mutex的进程间同步。

It would seem that creating a shared memory mapped file between processes, and through construction with InterlockedIncrement() one could create a largely usermode mutex akin to a CRITICAL_SECTION, which would be considerably more performant than the Win32 Mutex for interprocess synchronisation.

所以我的期望是它可能是在Win32的实现 boost :: interprocess_mutex 以这种方式实现,并且比本地API产品快得多。

So my expectation is that it may be probably for the implementation on Win32 of boost::interprocess_mutex to have been implemented in this manner, and for it to be substantially quicker than the native API offering.

我只有一个假设,我不通过现场测试知道 boost :: interprocess_mutex 的性能是用于进程间同步还是深入调查其实现。

I only however have a supposition, I don't know through field testing what the performance of the boost::interprocess_mutex is for interprocess synchronisation, or deeply investigated its implementation.

有没有人使用它或剖析其相对性能的经验,或者他们可以评论使用InterlockedIncrement()跨过使用共享内存的进程的安全吗?

Does anyone have experience in using it or profiling its relative performance, or can they comment on using the safety of using InterlockedIncrement() across processes using shared memory?

推荐答案

在boost 1.39.0中,只有对pthread的特定支持。在所有其他平台上,它成为一个繁忙循环,在中间进行yield调用(本质上与您描述的系统相同)。请参见boost / interprocess / sync / emulation / interprocess_mutex.hpp。例如,下面是lock()的实现:

In boost 1.39.0, there is only specific support for pthreads. On all other platforms, it becomes a busy-loop with a yield call in the middle (essentially the same system that you describe). See boost/interprocess/sync/emulation/interprocess_mutex.hpp. For example, here's the implementation of lock():

inline void interprocess_mutex::lock(void)
{
   do{
      boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);

      if (m_s == 1 && prev_s == 0){
            break;
      }
      // relinquish current timeslice
      detail::thread_yield();
   }while (true);
}

这意味着一个竞争的boost :: interprocess :: mutex是非常昂贵 - 虽然无争议的情况几乎是免费的。这可能可以通过添加一个事件对象或类似的睡眠来改善,但是这不适合boost :: interprocess的API,因为没有地方可以放置每个进程的HANDLE访问互斥体。

What this means is that a contended boost::interprocess::mutex on windows is VERY expensive - although the uncontended case is almost free. This could potentially be improved by adding an event object or similar to sleep on, but this would not fit well with boost::interprocess's API, as there would be nowhere to put the per-process HANDLE needed to access the mutex.

这篇关于boost :: interprocess_mutex vs Win32 native mutexes的性能是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆