c11 中的多线程支持

发布于 2024-12-27 02:47:53 字数 842 浏览 0 评论 0原文

新的 C11 标准提供了对多线程的支持。
我的问题有点多样化,但绝对可以回答。
我看过 C11 n1570< /a>草稿。
它说:

支持多线程执行,包括改进的内存排序 模型、原子对象和线程本地存储(

什么是改进的内存排序模型< /强>? c99 标准有何变化/有何变化?

如果有人更深入地研究它们并尝试解释所涉及的语义,我将不胜感激,而不仅仅是引用标准。

据我了解,C11 提供以下支持:

  • 线程创建和管理
  • 互斥
  • 体 条件变量
  • 线程特定存储和存储
  • 原子对象

我希望我没有错过任何东西?
既然现在标准库本身提供(将提供)多线程所需的所有功能,将来就不再需要 POSIX 和此类库(用于多线程支持)了吗?

最后,哪些编译器提供对上述功能的支持?是否有关于支持这些功能的时间表的参考信息?
我记得 C++11 有一个编译器支持和功能的链接,也许是类似的东西?

The new C11 standard provides a support for Multi-Threading.
My Questions are a bit diversified but definitely answerable.
I have had a look at the C11 n1570 draft.
It says:

support for multiple threads of execution including an improved memory sequencing
model, atomic objects, and thread-local storage (<stdatomic.h> and <threads.h>)

What is the Improved memory sequencing model? How/What changes from the c99 Standard?

Rather than just quotes from standard, I will appreciate if someone delves deeper in them and tries explaining the semantics involved.

As I understand, C11 provides support for:

  • Thread creation and Management
  • Mutex
  • Conditional Variables
  • Thread Specific storage &
  • Atomic Objects

I hope I didn't miss anything?
Since now the Standard library itself provides(will provide) all the functionalities needed for Multi-Threading, there would be no need for POSIX and such libraries(for Multi-Threading support) in future?

Lastly, What compilers provide support for the above mentioned features? Are there any references as to timelines when these will be supported?
I remember for C++11 there was a link for compiler support and features, perhaps something like that?

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

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

发布评论

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

评论(3

羁拥 2025-01-03 02:47:54

首先,不要放弃 C++11。新标准的并发工作是在 C++11 框架下完成的,然后导入到 C11 中,明确的目标是兼容。虽然存在一些语法差异(例如,由于普通 C 没有模板或函数重载),但从语义上讲,它们在设计上是相同的。要了解这一点的“证据”,可以查看 WG14 论文。例如:

及其参考文献。更多信息可以在以下位置找到:
打开 Std 网站

现在,回答您的问题:

什么是改进的内存排序模型?

显而易见的答案是它已被更改以考虑多个线程及其交互方式。有关稍长的答案,请参阅 C++11引入了标准化内存模型。这是什么意思?它将如何影响 C++ 编程? 这已经在评论中提到了。为了深入理解,stackoverflow 答案可能不是正确的地方(更不用说带有多个子问题的问题了!)。但幸运的是 Hans Boehm 维护了一个非常好的页面,其中包含可供进一步阅读的有趣链接(再次,请保留请记住,C11 和 C++11 内存模型在语义上是相同的)

我希望我没有错过任何内容?

连同内存模型,您的列表似乎涵盖了 C11 中的并发添加。对于其他更改,维基百科有一个列表;我无法想出维基百科列表中遗漏的任何内容。

既然现在标准库本身提供(将提供)多线程所需的所有功能,将来就不再需要 POSIX 和此类库(用于多线程支持)了吗?

是的,有将是他们的需要。首先,没有人会重写使用各种现有线程 API 的所有现有代码。其次,C(++)11 线程库很可能被实现为各种本机线程库的包装器;哎呀,甚至还有一种记录在案的方法来检索指向底层本机线程的指针,以防万一需要执行超出 C(++) 线程库支持范围的操作。将 C(++)11 线程库视为各种本机线程库的可移植、最小公分母包装器。

最后,哪些编译器提供对上述功能的支持?是否有关于这些支持的时间表的参考?我记得 C++11 有一个编译器支持和功能的链接,也许是类似的东西?

我还没有看到任何详细列表,与 C 相比,关于 C11 的讨论似乎没有那么多++11。这里有关于即将推出的 GCC 4.7 的简短通知:http://gcc.gnu.org/ gcc-4.7/changes.html 。对于并发支持,可以在 C++11 状态页面中检查并发支持: http ://gcc.gnu.org/projects/cxx0x.html。还有一些关于 GCC 当前状态和计划的说明,位于 http://gcc.gnu.org/wiki/ Atomic(根据该页面,stdatomic.h 可用)。对于其他编译器,这里有一个关于各种编译器的 C++11 状态的不错列表 http://www.aristeia.com/C++11/C++11FeatureAvailability.htm 。从那里的链接可以检查并发支持的状态,假设相关供应商计划支持 C11,那么 C11 并发支持可能处于大致相同的水平。

First, don't write off C++11. The concurrency work for the new standards was done under the C++11 umbrella, then imported into C11 with the explicit goal of being compatible. While there are some syntactical differences (e.g. due to plain C not having templates or function overloading), semantically they are identical by design. For "evidence" of this, one can check the WG14 papers. E.g:

and references therein. More can be found at
Open Std Website

Now, on to your questions:

What is the Improved memory sequencing model?

The obvious answer is that it has been changed to take into account multiple threads and how they interact. For a slightly longer answer, see C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming? that was already mentioned in the comments. For an in-depth understanding, a stackoverflow answer is perhaps not the right place (even less a question with several subquestions!). But luckily Hans Boehm maintains a very good page with interesting links for further reading (again, keep in mind that the C11 and C++11 memory models are semantically identical)

I hope I didn't miss anything?

Together with the memory model, your list seems to cover the concurrency additions in C11. For other changes, wikipedia has a list; of the top of my head I can't come up with anything the wikipedia list has missed.

Since now the Standard library itself provides(will provide) all the functionalities needed for Multi-Threading, there would be no need for POSIX and such libraries(for Multi-Threading support) in future?

Yes, there will be a need for them. First, nobody is going to rewrite all the existing code that uses the various existing thread API's. Secondly, the C(++)11 thread library is/will most likely be implemented as a wrapper around the various native thread libraries; heck, there's even a documented way to retrieve a pointer to the underlying native thread, in case one needs to do something beyond what the C(++) thread library supports. Think of the C(++)11 thread library more like a portable, least common denominator wrapper around the various native thread libraries.

Lastly, What compilers provide support for the above mentioned features? Are there any references as to timelines when these will be supported? I remember for C++11 there was a link for compiler support and features, perhaps something like that?

I haven't seen any detailed list, there doesn't seem to be as much buzz around C11 compared to C++11. There's a short notice for the upcoming GCC 4.7 here: http://gcc.gnu.org/gcc-4.7/changes.html . For the concurrency support, one can check the support for concurrency in the C++11 status page here: http://gcc.gnu.org/projects/cxx0x.html . There's also some notes on the current status and plans for GCC at http://gcc.gnu.org/wiki/Atomic (according to that page, stdatomic.h is available). For other compilers, there's a nice list of the C++11 status for various compilers here http://www.aristeia.com/C++11/C++11FeatureAvailability.htm . From the links there one can check the status of the concurrency support, and assuming that the vendor in question plans to support C11, the C11 concurrency support is then likely to be at about the same level.

筱果果 2025-01-03 02:47:54

关于哪些编译器提供对上述功能的支持?


Pelles C supports C11 <threads.h>. Creation of threads with Pelles C compiler example:

#include <stdio.h>
#include <threads.h>

#define NUM_THREADS 7

static int threadData[NUM_THREADS];

int threadFunction(void * data) {
    printf("%d-th thread up\n", *(int*)data);
    return 0;
}

int main(void) {
    thrd_t threadId[NUM_THREADS];

    // init thread data
    for (int i=0; i < NUM_THREADS; ++i)
        threadData[i] = i;

    // start NUM_THREADS amount of threads
    for (int i=0; i < NUM_THREADS; ++i) {
        if (thrd_create(threadId+i, threadFunction, threadData+i) != thrd_success) {
            printf("%d-th thread create error\n", i);
            return 0;
        }
    }

    // wait until all threads terminates
    for (int i=0; i < NUM_THREADS; ++i)
        thrd_join(threadId[i], NULL);

    return 0;
}

编辑:消除了线程共享数据问题以及在所有线程终止之前退出main()的问题。

Regarding What compilers provide support for the above mentioned features?


Pelles C supports C11 <threads.h>. Creation of threads with Pelles C compiler example:

#include <stdio.h>
#include <threads.h>

#define NUM_THREADS 7

static int threadData[NUM_THREADS];

int threadFunction(void * data) {
    printf("%d-th thread up\n", *(int*)data);
    return 0;
}

int main(void) {
    thrd_t threadId[NUM_THREADS];

    // init thread data
    for (int i=0; i < NUM_THREADS; ++i)
        threadData[i] = i;

    // start NUM_THREADS amount of threads
    for (int i=0; i < NUM_THREADS; ++i) {
        if (thrd_create(threadId+i, threadFunction, threadData+i) != thrd_success) {
            printf("%d-th thread create error\n", i);
            return 0;
        }
    }

    // wait until all threads terminates
    for (int i=0; i < NUM_THREADS; ++i)
        thrd_join(threadId[i], NULL);

    return 0;
}

EDIT: Eliminated thread shared data problem and problem of exiting from main() earlier than all threads terminates.

小姐丶请自重 2025-01-03 02:47:54

Janneb 已经给出了很多解释。对于您的最后一个问题

最后,哪些编译器提供了对上述内容的支持
特征?有没有关于这些的时间表的参考资料
支持吗?

gcc 系列编译器(clang、icc、opencc)支持新标准所需的大部分语义,仅存在语法差异。 (clang甚至在最新版本中实现了_Generic。)

对于P99我有编写的包装宏将大多数功能映射到已经是 C11 语法,或者接近它(用于模拟 _Generic)。

因此,如果您有这些编译器之一并且位于 POSIX 系统上,您可以立即开始使用大量(大多数)C11:具有所有类型 mtx_h 的线程等、_Atomic 的原子、类型通用宏(语法与 C11 略有不同)、_Static_assert 和对齐内容。

Janneb already has given a lot of explanations. For your last questions

Lastly, What compilers provide support for the above mentioned
features? Are there any references as to timelines when these will be
supported?

The gcc family of compilers (clang, icc, opencc) supports most of the semantics that the new standard requires, there are only syntactical differences. (clang even implements _Generic in the latest version.)

For P99 I have written wrapper macros that map most of the features to something that is already C11 syntax, or comes close to it (for emulating _Generic).

So if you have one of these compilers and are on a POSIX system, you can start to use a lot (most) of C11 immediately: threads with all the types mtx_h etc, atomics with _Atomic, type generic macros (syntax is slightly different from C11), _Static_assert and the alignment stuff.

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