gpt4 book ai didi

c++ - static combinable 可以用作 thread_local 的占位符吗?

转载 作者:行者123 更新时间:2023-11-30 03:10:08 26 4
gpt4 key购买 nike

C++0x 添加了一个新的存储说明符 thread_local 尚未在 VS10 中实现。

但是并行编程库定义了一个 Concurrency::combinable class它有一个 local() 返回对线程私有(private)子计算的引用的函数

thread_local 有语义吗? static 不能(轻易)覆盖combinable<T> 类型的变量?

如果不是,为什么是 thread_local添加到核心语言是否可以在库中实现?

最佳答案

为什么 C++ 添加 class 对象是否可以像 GObject 在 C 中那样由库实现?因为 C++ 希望 class 对象在编译时 是已知的,所以这样效率更高。因此 C++ 类初始化/取消初始化是异常安全的 ( RAII )。

一些功能比较重要,需要更多的实现。 thread_local 是一个存储类说明符,因此可以在编译时获知,编译器可以进行更多优化。

也在 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2659.htm 中曾说过:

Thread Variable Dynamic Initialization

At present, all implementations of thread-local storage do not support dynamic initalization (and presumably non-trivial destructors). There was mild consensus at the Mont Treblant meeting to support dynamic initialization of function-local, thread-local variables. The intialization of such variables is already guarded and synchronous, so new technology is not required. On the other hand, the implementation for dynamic initialization of namespace-scope variables is much more difficult, and may require additional linker and operating system support.

为什么 thread_local 被引入为新的语言关键字?看来原因可能是线程变量动态初始化

关于c++ - static combinable<T> 可以用作 thread_local 的占位符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3459368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com