Intel TBB run a function in a parallel thread?

断了今生、忘了曾经 提交于 2019-12-06 05:30:29

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.

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