2012-07-19 142 views
2

基本上我正在开发一个opencv应用程序。我已经在cmake中建立了OpenCVwith_tbb选项。英特尔TBB在并行线程中运行一个函数?

我想使用英特尔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()

回答

2

英特尔TBB并不打算用于这种目的。从Tutorial 报价:

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

你想做的事可与boost::thread或C++ 11线程功能,来轻松achived事情:

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

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