简体   繁体   English

如何在经典编译器中使用std :: atomic或boost :: atomic?

[英]How to use std::atomic or boost::atomic with the classic compiler?

I'm currently working on a mid-sized project in C++ Builder 10.1.2 where I am unfortunately tied to the BCC32 classic compiler (mainly for reliability and speed reasons). 我目前正在C ++ Builder 10.1.2中从事一个中等规模的项目,不幸的是,我在其中与BCC32 经典编译器绑定(主要是出于可靠性和速度方面的考虑)。

For some background parsing, it would be nice if I could use C++11 's std::atomic , but the classic compiler is not capable of that. 对于某些背景解析,如果可以使用C ++ 11std::atomic会很好,但是经典的编译器无法做到这一点。 Even boost::atomic is not available, because only boost V.1.39 can be installed for BCC32, which comes with no boost::atomic built-in. 甚至boost::atomic都不可用,因为BCC32只能安装boost V.1.39,没有内置boost::atomic

So... 所以...

  • Is there some possibility to use a newer boost version with the classic compiler? 有可能在经典编译器中使用更新的boost版本吗?

  • Are there some alternatives to ... ::atomic (except for mutex-wrapped normal variables)? ... ::atomic是否有其他替代方法(互斥体包装的普通变量除外)?

You can implement lock-less std::atomic yourself, provided you have access to assembly. 您可以自己实现无锁std::atomic ,前提是您可以访问程序集。 Just figure out the corresponding assembly instructions for your CPU and code them. 只需找出适用于您的CPU的相应组装指令并将其编码即可。 Easy way of doing this would be to compile some test code with std::atomic using modern compiler on your CPU and look into generated ASM. 简单的方法是在CPU上使用现代编译器使用std::atomic编译一些测试代码,并查看生成的ASM。 Of course, it would be CPU-dependent and you will use portability. 当然,这将取决于CPU,并且您将使用可移植性。

If your compiler doesn't allow you to use ASM or you do not want to marry yourself to particular CPU, you can rely on OS-provided API - both *nix and Windows has family of atomic functions. 如果您的编译器不允许您使用ASM,或者您不想与特定的CPU结婚,则可以依赖操作系统提供的API-* nix和Windows都具有一系列原子功能。

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

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