简体   繁体   English

便携式作家阅读器自旋锁

[英]Portable Writer-Readers spinlock

I used boost::shared_mutex as synchronization objects in Writer-Readers model. 我使用boost::shared_mutex作为Writer-Readers模型中的同步对象。 However, it's considerably slow compared with no locks used (In my test: without any readers, to write 500,000 values it takes 100 msec if no locks used and ~500 msec if boost::shared_mutex used). 但是,与不使用锁相比,它的速度相当慢(在我的测试中:没有任何读取器,要写入500,000个值,如果不使用锁,则需要100毫秒;如果使用boost::shared_mutex则需要约500毫秒)。 That's why I decided to consider something like spinlock. 这就是为什么我决定考虑使用自旋锁的原因。 I would like to make a portable spinlock (Windows + Linux). 我想制作一个便携式自旋锁(Windows + Linux)。

  1. Is there any boost wrapper that I can use instantly? 我可以立即使用任何增强包装吗?
  2. This is a good topic that shows how to implement spinlock on Windows. 是一个很好的主题,它显示了如何在Windows上实现自旋锁。 But I was unable to find out equivalents of Win32 APIs such as InterlockedCompareExchange , YieldProcessor , InterlockedDecrement on Linux. 但我无法找到Win32 API的等效项,例如Linux上的InterlockedCompareExchangeYieldProcessorInterlockedDecrement

Update 2016/03/08 更新2016/03/08

I found out a guide and the implementation released by facebook on github. 我找到了一个指南,并在github上由facebook发布了实现 Haven't tried it yet. 还没有尝试过。 I updated here for anyone who will have the same question and might want to refer. 我在这里为有相同问题并可能希望参考的任何人更新。

  1. There exists no portable solution for C++03 because C++03 has no memory model but you can implement it very easily by just copying atomic_flag implementations of your target platform. C ++ 03没有可移植的解决方案,因为C ++ 03没有内存模型,但是您可以通过复制目标平台的atomic_flag实现非常容易地实现它。

  2. My comment for the given link/tutorial: volatile should not be used by an application programmer unless you access unusual memory. 我对给定链接/教程的评论:除非您访问异常内存,否则应用程序程序员不应使用volatile I recommend this and all others articles of that topic. 我推荐主题的所有文章。 There should be a ready to use library for download. 应该有一个可供使用的库可供下载。 I do not know if it is C++11 only. 我不知道是否只有C ++ 11。

  3. I assume you are not very experienced with multithreading, synchronization and locks. 我假设您对多线程,同步和锁没有很丰富的经验。 Whatever you want to achieve, just go the standard route first and came back later if your performance is not enough. 无论您要实现什么目标,只要您的性能不够,就先走标准路线,然后再返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM