简体   繁体   English

C ++中的Mutex在C ++之前11

[英]Mutex in C++ BEFORE C++11

Question is simple - what are simple, ready-to-use solutions for mutexes in C++ before C++11 ? 问题很简单 - 在C ++ 11之前,什么是C ++中互斥体的简单,现成的解决方案? I need nothing fancy - just some bit of code to wait for another callback (that may happen anytime) to finish before executing. 我不需要任何花哨 - 只需要一些代码来等待另一个回调(可能随时发生)在执行之前完成。

I'm running on your everyday PC, i7 and whatnot. 我正在你的日常电脑上运行,i7和诸如此类的东西。 No specific performance requirements (simply not too slow), but must do the job of stopping one process and let the other finish and then resume the first one. 没有具体的性能要求(根本不是太慢),但必须完成停止一个过程的工作,让另一个完成然后恢复第一个过程。

Thanks in advance ! 提前致谢 !

Boost Synchronization is surely a valid option. Boost Synchronization肯定是一个有效的选择。

If you don't want to use Boost (based on your question you may find it overkill for your needs), you can write your own c++ class wrapper around the API provided by your target platform (ie POSIX thread mutex for POSIX compliant platforms, or Windows Mutexes or Critical Sections on Microsoft Windows OS). 如果您不想使用Boost(基于您的问题,您可能会发现它满足您的需求),您可以围绕目标平台提供的API编写自己的c ++类包装器(即POSIX兼容平台的POSIX线程互斥 ,或Microsoft Windows OS上的Windows MutexCritical Sections )。

There are surely a lot of such wrapper class already implemented by someone else, ie you can try searching on GitHub using the mutex keyword and restricting results for code and C++ language ( here are a lot of results ). 肯定有很多这样的包装类已经由其他人实现,即你可以尝试使用mutex关键字在GitHub上搜索并限制代码和C ++语言的结果这里有很多结果 )。

You might look not only in Boost, but also into POCO (its Mutex class) or Qt (its QMutex class). 你不仅可以看到Boost,还可以看到POCO (它的Mutex类)或Qt (它的QMutex类)。

If you target only POSIX pthreads , writing your own mutex class above its standard mutexes is doable and has been done a lot of times. 如果你只定位POSIX pthreads ,那么在你的标准互斥锁之上编写你自己的互斥锁类是可行的,而且已经做了很多次。

However, switching to C++11 is probably the best approach in 2017. Notice that recent GCC or Clang/LLVM are supporting many (embedded, or tablet/laptop/desktop/server) processors. 但是,切换到C ++ 11可能是2017年最好的方法。请注意, 最近的 GCCClang / LLVM支持许多(嵌入式或平板电脑/笔记本电脑/台式机/服务器)处理器。

(so it might worth the effort - or money - to get somehow a recent GCC or Clang, perhaps by compiling these compilers from their source code, or paying someone to do that work) (因此,可能值得付出努力 - 或金钱 - 以某种方式获得最近的GCC或Clang,可能是通过从源代码编译这些编译器,或付某人来完成这项工作)

You should explain what computer (processor, operating system if any, available memory) you are targetting. 您应该解释您要定位的计算机(处理器,操作系统,如果有,可用内存)。 Even if POCO or Qt or Boost can be compiled in C++03 mode, they might not fit on a tiny embedded processor (eg some Arduino). 即使POCO或Qt或Boost可以在C ++ 03模式下编译,它们也可能不适合微型嵌入式处理器(例如某些Arduino)。

Use pthreads on posix (most Unices) systems. 在posix(大多数Unices)系统上使用pthreads。 On Windows, your best bet are CriticalSections. 在Windows上,最好的选择是CriticalSections。

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

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