Basically I am developing an opencv application. I have built OpenCV with_tbb
option in cmake
.
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;
}
}
How can I run secondaryThreadFunction()
on another thread ?
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.
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.
来源:https://stackoverflow.com/questions/11563502/intel-tbb-run-a-function-in-a-parallel-thread