简体   繁体   English

C89 的原子当量

[英]Atomic equivalent for C89

So, im programming in C89, and its going well so far except one issue, Im doing multithreaded applications and I need to use atomic.所以,我在 C89 中编程,到目前为止它进展顺利,除了一个问题,我在做多线程应用程序,我需要使用原子。

I dont want to switch to C11 because I want my code to be compatable on every compiler and system and for my code to last a very long time.我不想切换到 C11,因为我希望我的代码在每个编译器和系统上都兼容,并且我的代码可以持续很长时间。

Iv'e searched through stackoverflow for an existing question on this topic but didn't find any questions.我通过 stackoverflow 搜索了有关此主题的现有问题,但没有找到任何问题。

Does anyone know how to use the Atomic in C89.有谁知道如何在 C89 中使用 Atomic。 Say I have two threads using a bool假设我有两个使用 bool 的线程

#include <stdatomic.h>
_Atomic bool theBool = false;

void funFromThirstThread()
{
    theBool = true;
}

void funFromSecondThread() /*gets called repeatedly*/
{
    if(theBool)
    {
        /*Do something*/
    }
}

The above code is what I would do in C11, using the atomic in that, but how would I do this in C89?上面的代码是我在 C11 中要做的,在其中使用原子,但我将如何在 C89 中做到这一点? Can this be done?这可以做到吗? Preferably without volatile and locks thanks.最好没有易失性和锁定谢谢。

It can't be done.这是不可能的。

Prior to C11, to get atomic operations, you had to use inline assembler or compiler-specific intrinsics to access the appropriate instructions.在 C11 之前,要获得原子操作,您必须使用内联汇编程序或编译器特定的内在函数来访问适当的指令。 And since the language had no formal memory model, you had to rely on knowledge of compiler-specific internals (often undocumented) to know what optimizations it would or wouldn't perform in what contexts.而且由于该语言没有正式的 memory model,因此您必须依靠特定于编译器的内部知识(通常未记录)来了解它会或不会在什么上下文中执行哪些优化。 Or else, throw around a lot of volatile s and cross your fingers.否则,扔掉很多volatile s 并交叉手指。 Sometimes both.有时两者兼而有之。 Nothing was portable in any way, and subtle bugs were common.没有任何东西是可移植的,而且细微的错误很常见。

If there had been a reliable and portable way to use atomics prior to C11, then C11 probably wouldn't have bothered to include them.如果在 C11 之前有一种可靠且可移植的方式来使用原子,那么 C11 可能不会费心包含它们。 There is a very good reason why they did.他们这样做是有充分理由的。

Per the comments, you say you are using the plibsys library for threads, and UnholySheep points out that it also has support for atomics.根据评论,您说您正在将 plibsys 库用于线程,并且 UnholySheep 指出它还支持原子。 So you should probably just use those.所以你可能应该只使用那些。 Still, though, keep in mind that a generic C89 compiler doesn't make any promises to avoid optimizations that would break the required memory ordering.不过,请记住,通用 C89 编译器不会做出任何承诺来避免会破坏所需的 memory 排序的优化。 Usually they were not smart enough to do such optimizations in the first place, but everything is much more at your own risk.通常,他们一开始就不够聪明,无法进行此类优化,但一切都需要您自担风险。

I dont want to switch to C11 because I want my code to be compatible on every compiler and system and for my code to last a very long time.我不想切换到 C11,因为我希望我的代码在每个编译器和系统上都兼容,并且我的代码可以持续很长时间。

That goal is basically unattainable for any program more complex than "Hello World".对于任何比“Hello World”更复杂的程序,这个目标基本上是无法实现的。 But my feeling is that using C11 gets you closer to it, not further away.但我的感觉是,使用 C11 可以让你更接近它,​​而不是更远。

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

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