【问题标题】:Intel TBB run a function in a parallel thread?英特尔 TBB 在并行线程中运行函数?
【发布时间】:2012-07-19 14:53:25
【问题描述】:

基本上我正在开发一个 opencv 应用程序。我在cmake 中构建了 OpenCV with_tbb 选项。

我想使用 intel tbb 运行一个并行线程,该线程每隔一段时间会更新一些全局变量。比如:

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()

【问题讨论】:

    标签: c++ multithreading opencv tbb


    【解决方案1】:

    英特尔 TBB 不适合用于此类目的。 引用自Tutorial

    英特尔® 线程构建模块以线程为目标,以提高性能。 大多数通用线程包支持许多不同的种类 线程,例如图形中异步事件的线程 用户界面。因此,通用包往往是 提供基础而非解决方案的低级工具。反而, 英特尔® 线程构建模块专注于以下特定目标: 并行计算密集型工作,提供更高级别, 更简单的解决方案。

    使用boost::thread 或 C++11 线程功能可以轻松实现您想做的事情:

       // 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();
    

    请记住同步对任何共享变量的访问。

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多