C++11 中的 TBB 等价物
TBB equivalants in C++11
我有一个旧代码库,我想在新环境中使用其中的一些实现。老基地用的是我很陌生的TBB框架
在 C++11 中是否有这些 TBB 类型的等效实现:
- tbb::enumerable_thread_specific<...>
- mutex_t
- mutex_t::scoped_lock
如果没有:我可以如何转换它们的任何提示(链接到良好的 TBB 摘要、教程等),或者我是否需要自己完成整个 TBB 文档?
(不。将 TBB 插入项目不是一个选项。)
编辑:忘记提及 tbb::this_tbb_thread::yield 对此有何建议?
我建议先让旧代码库正常工作,然后再进行更改。
tbb::enumerable_thread_specific<...>
没有标准的等价物。
mutex_t
和 mutex_t::scoped_lock
可以替换为 std::mutex
and std::unique_lock<std::mutex>
.
您代码中的 TBB 功能在 C++11 中确实具有近似等价物(或者您可以简单地创建一个)。
enumerable_thread_specific<T>
is an implementation of thread-local storage. It can use the platform's local storage, or a tbb::concurrent_vector
to hold instances. The default is to consume no platform thread-local storage keys. C++11 has the thread_local
限定符,因此根据 enumerable_thread_specific
的使用方式,您可以将其替换为相同类型的 thread_local
版本。如果您使用该结构来保存数据,或在线程本地上下文之外访问它,您的工作可能会被切断。
mutex_t
是通用的互斥类型,可以用 std::mutex
代替,尽管开发人员可能有 chosen a particular implementation(如 spin_mutex
)替换。
scoped_lock
如果您使用的是 C++17,则 RAII object that locks the mutex on construction, and when the leaving the scope will unlock the mutex (making it somewhat exception-friendly.) You can use std::lock_guard<std::mutex>
是一个 RAII object that locks the mutex on construction, and when the leaving the scope will unlock the mutex (making it somewhat exception-friendly.) You can use std::lock_guard<std::mutex>
,否则您可以自己滚动。
- 我已经有一段时间没有阅读 yield 文档了。我相信在放弃时间片之前,实现会寻找其他可能的任务。您可以使用
std::this_thread::yield()
放弃时间片,但如果代码使用 TBB 构造,行为可能会有所不同。事实上你没有提到任何其他 TBB 的东西对我来说意味着程序中有 none,tbb::yield()
和 std::this_thread::yield()
. 做同样的事情
我有一个旧代码库,我想在新环境中使用其中的一些实现。老基地用的是我很陌生的TBB框架
在 C++11 中是否有这些 TBB 类型的等效实现:
- tbb::enumerable_thread_specific<...>
- mutex_t
- mutex_t::scoped_lock
如果没有:我可以如何转换它们的任何提示(链接到良好的 TBB 摘要、教程等),或者我是否需要自己完成整个 TBB 文档?
(不。将 TBB 插入项目不是一个选项。)
编辑:忘记提及 tbb::this_tbb_thread::yield 对此有何建议?
我建议先让旧代码库正常工作,然后再进行更改。
tbb::enumerable_thread_specific<...>
没有标准的等价物。
mutex_t
和 mutex_t::scoped_lock
可以替换为 std::mutex
and std::unique_lock<std::mutex>
.
您代码中的 TBB 功能在 C++11 中确实具有近似等价物(或者您可以简单地创建一个)。
enumerable_thread_specific<T>
is an implementation of thread-local storage. It can use the platform's local storage, or atbb::concurrent_vector
to hold instances. The default is to consume no platform thread-local storage keys. C++11 has thethread_local
限定符,因此根据enumerable_thread_specific
的使用方式,您可以将其替换为相同类型的thread_local
版本。如果您使用该结构来保存数据,或在线程本地上下文之外访问它,您的工作可能会被切断。mutex_t
是通用的互斥类型,可以用std::mutex
代替,尽管开发人员可能有 chosen a particular implementation(如spin_mutex
)替换。scoped_lock
如果您使用的是 C++17,则 RAII object that locks the mutex on construction, and when the leaving the scope will unlock the mutex (making it somewhat exception-friendly.) You can usestd::lock_guard<std::mutex>
是一个 RAII object that locks the mutex on construction, and when the leaving the scope will unlock the mutex (making it somewhat exception-friendly.) You can usestd::lock_guard<std::mutex>
,否则您可以自己滚动。- 我已经有一段时间没有阅读 yield 文档了。我相信在放弃时间片之前,实现会寻找其他可能的任务。您可以使用
std::this_thread::yield()
放弃时间片,但如果代码使用 TBB 构造,行为可能会有所不同。事实上你没有提到任何其他 TBB 的东西对我来说意味着程序中有 none,tbb::yield()
和std::this_thread::yield()
. 做同样的事情