C89的原子量

发布于 2025-02-12 09:38:20 字数 515 浏览 2 评论 0原文

因此,IM编程在C89中,除了一个问题之外,它的进展顺利,我需要进行多线程应用程序,我需要使用原子。

我不想切换到C11,因为我希望我的代码在每个编译器和系统上都可以兼容,并且代码可以持续很长时间。

IV'E通过Stackoverflow搜索了有关此主题的现有问题,但没有找到任何问题。

有人知道如何在C89中使用原子。 假设我有两个线程使用bool,

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

void funFromThirstThread()
{
    theBool = true;
}

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

上面的代码是我在C11中使用的原子,但是我该如何在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.

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.

Iv'e searched through stackoverflow for an existing question on this topic but didn't find any questions.

Does anyone know how to use the Atomic in C89.
Say I have two threads using a 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? Can this be done? Preferably without volatile and locks thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2025-02-19 09:38:20

不能完成。

在获得C11之前,要获取原子操作,您必须使用内联汇编器或编译器特定的内在系统来访问适当的说明。而且,由于该语言没有正式的内存模型,因此您必须依靠针对编译器的内部设备(通常是无证件)的知识来知道它将在什么情况下进行或不会执行什么优化。否则,扔掉很多挥发性 s并交叉手指。有时两者兼而有之。任何东西都没有任何便携式,细微的虫子很常见。

如果在C11之前有一种可靠且可移植的方法使用原子,那么C11可能不会费心包括它们。他们这样做有很好的理由。

根据评论,您说您正在使用plibsys库作为线程,毫无疑问地指出,它也支持原子。因此,您可能应该只使用这些。不过,请记住,通用的C89编译器并没有做出任何承诺,以避免优化会破坏所需的内存顺序。 通常他们不够聪明,无法首先进行此类优化,但是一切都更加自负。

我不想切换到C11,因为我希望我的代码在每个编译器和系统上兼容,并且代码可以持续很长时间。

对于比“ Hello World”更复杂的任何程序,这个目标基本上是无法实现的。但是我的感觉是,使用C11使您更接近它,​​而不是更远。

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. 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. Or else, throw around a lot of volatiles and cross your fingers. 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. 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. 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. 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.

That goal is basically unattainable for any program more complex than "Hello World". But my feeling is that using C11 gets you closer to it, not further away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文