在c99中使用__thread

发布于 2024-11-27 01:43:24 字数 155 浏览 0 评论 0原文

我想使用 __thread 存储类定义一些特定于线程的变量。但三个问题让我犹豫:

  1. 它真的是c99中的标准吗?或者更重要的是,编译器支持有多好?
  2. 变量会在每个线程中初始化吗?
  3. 非多线程程序是否将它们视为普通的旧全局变量?

I would like to define a few variables as thread-specific using the __thread storage class. But three questions make me hesitate:

  1. Is it really standard in c99? Or more to the point, how good is the compiler support?
  2. Will the variables be initialised in every thread?
  3. Do non-multi threaded programs treat them as plain-old-globals?

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

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

发布评论

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

评论(2

傾城如夢未必闌珊 2024-12-04 01:43:24

回答您的具体问题:

  1. 不,它不是 C99 的一部分。您不会在 n1256.pdf (C99+TC1/2/3) 或原始 C99 标准中的任何地方找到它。
  2. 是的,__thread 变量在每个新线程中都以其初始化值开始。
  3. 从程序行为的角度来看,线程局部存储类变量的行为与非多线程程序中的普通全局变量几乎相同。然而,它们确实会产生更多的运行时成本(内存和启动时间),并且可能存在线程局部变量的大小和数量限制的问题。所有这些都相当复杂,并且根据您的程序是静态链接还是动态链接以及变量是否驻留在主程序中还是共享库中而有所不同......

除了实现 C/POSIX 之外(例如 errno 等),在我看来,线程本地存储类实际上不是很有用。它几乎是避免以上下文指针或类似形式干净地传递必要状态的拐杖。您可能认为它对于解决像 qsort 这样不采用​​上下文指针的损坏接口很有用,但不幸的是,不能保证 qsort 会调用比较函数在调用 qsort 的同一线程中。它可能会分解作业并在多个线程中运行它。对于大多数其他可以使用这种解决方法的接口也是如此。

To answer your specific questions:

  1. No, it is not part of C99. You will not find it mentioned anywhere in the n1256.pdf (C99+TC1/2/3) or the original C99 standard.
  2. Yes, __thread variables start out with their initialized value in every new thread.
  3. From a standpoint of program behavior, thread-local storage class variables behave pretty much the same as plain globals in non-multi-threaded programs. However, they do incur a bit more runtime cost (memory and startup time), and there can be issues with limits on the size and number of thread-local variables. All this is rather complicated and varies depending on whether your program is static- or dynamic-linked and whether the variables reside in the main program or a shared library...

Outside of implementing C/POSIX (e.g. errno, etc.), thread-local storage class is actually not very useful, in my opinion. It's pretty much a crutch for avoiding cleanly passing around the necessary state in the form of a context pointer or similar. You might think it could be useful for getting around broken interfaces like qsort that don't take a context pointer, but unfortunately there is no guarantee that qsort will call the comparison function in the same thread that called qsort. It might break the job down and run it in multiple threads. Same goes for most other interfaces where this sort of workaround would be possible.

弃爱 2024-12-04 01:43:24

您可能想阅读以下内容:

http://www.akkadia.org/drepper/tls.pdf< /a>

1) MSVC 不支持 C99。 GCC 确实和其他编译器尝试 GCC 兼容性。

编辑编译器对__thread支持的详细信息可在此处找到:

http://chtekk.longitekk.com/index.php?/archives/2011/02/C8.html

2) 仅 C++ 支持初始化程序并且它必须是恒定的。

3)非多线程应用程序是单线程应用程序。

You probably want to read this:

http://www.akkadia.org/drepper/tls.pdf

1) MSVC doesn't support C99. GCC does and other compilers attempt GCC compatibility.

edit A breakdown of compiler support for __thread is available here:

http://chtekk.longitekk.com/index.php?/archives/2011/02/C8.html

2) Only C++ supports an initializer and it must be constant.

3) Non-multi-threaded applications are single-threaded applications.

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