英特尔TBB在并行线程中运行一个函数? [英] Intel TBB run a function in a parallel thread?
问题描述
基本上我正在开发一个opencv应用程序。我已在 cmake
中建立 OpenCV with_tbb
选项。
Basically I am developing an opencv application. I have built OpenCV with_tbb
option in cmake
.
我想使用intel tbb来运行并行线程,在某些时间间隔更新一些全局变量。像:
I would like to use intel tbb to run a parallel thread that at some intervals updates some global variables. Something like:
vector<int> mySharedVar;
void secondaryThreadFunction() {
while(true) {
Do some operations
And update mySharedVar if necessarily
usleep(1000);
}
}
int main() {
run in parallel secondaryThreadFunction;
in the master Thread keep doing something based on mySharedVar
while(true) {
do something;
}
}
如何运行 secondaryThreadFunction ()
How can I run secondaryThreadFunction()
on another thread ?
推荐答案
Intel TBB不是用于这种类型的目的。
来自教程的引语:
Intel TBB is not meant to be used for that kind of purpose. Quote from the Tutorial:
英特尔®线程构建模块的目标是提高性能。
大多数通用线程程序包支持许多不同类型的
线程,例如图形化
用户界面中的异步事件线程。因此,通用程序包往往是提供基础而不是解决方案的
低级工具。相反,
英特尔®线程构建块专注于
的并行化计算密集型工作的特定目标,提供更高级别的
更简单的解决方案。
Intel® Threading Building Blocks targets threading for performance. Most general-purpose threading packages support many different kinds of threading, such as threading for asynchronous events in graphical user interfaces. As a result, general-purpose packages tend to be low-level tools that provide a foundation, not a solution. Instead, Intel® Threading Building Blocks focuses on the particular goal of parallelizing computationally intensive work, delivering higher-level, simpler solutions.
你想做的事情可以很容易地用 boost :: thread
或者C ++ 11的线程特性:
The thing you wanna do can be easily achived with either boost::thread
or the C++11 threading features:
// using async
auto fut = std::async(std::launch::async, secondaryThreadFunction);
// using threads (for boost, just replace std with boost)
std::thread async_thread(secondaryThreadFunction);
in the master Thread keep doing something based on mySharedVar
while(true) {
do something;
}
// in case of using threads
async_thread.join();
请记住,将访问权限同步到任何共享变量。
Keep in mind to sync the access to any shared variables.
这篇关于英特尔TBB在并行线程中运行一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!