2012-07-19 112 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(); 

記住同步的訪問任何共享變量。