Intel TBB run a function in a parallel thread?

一曲冷凌霜 提交于 2019-12-08 00:44:48

问题


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 ?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!