英特尔TBB在并行线程中运行函数
Intel TBB run a function in a parallel thread?
基本上我正在开发一个opencv应用程序。我在cmake
中构建了OpenCVwith_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()
?
英特尔TBB并非用于此类目的。引用教程:
"英特尔®;线程构建基块"以线程为目标以提高性能。大多数通用线程包支持许多不同的类型线程,例如图形中异步事件的线程用户界面。因此,通用程序包往往提供基础而非解决方案的低级工具。相反"英特尔®;线程构建基块"专注于并行化计算密集型工作,更简单的解决方案。
使用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();
请记住同步对任何共享变量的访问。
相关文章:
- 无法获取菜单选择以运行函数.C++
- 为什么std::async使用同一个线程运行函数
- MINGW - 正确运行函数所需的 cdecl
- 如何在 C 中使用空的 main() 方法运行函数?
- 运行函数作为 constexpr 和不作为 constexpr
- 如何在全局变量的构造函数之前运行函数
- 在 Cap'n Proto RPC 服务器中定期运行函数
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- CLang:在 std::thread 中运行函数会导致结构创建BAD_ACCESS
- 如何使用 TBB 在单个线程中运行函数
- 我在运行函数 GetVolumeInformation() 时得到非常随机的结果
- 如何使用 boost::asio io_service 运行函数异步
- Qt - 在其他线程上运行函数
- 在一组模板化对象上运行函数
- pybind11:属性错误:尝试从 py 文件运行函数时,模块'XXX'没有属性'YYY'
- 添加按钮以通知运行函数的通知
- 如何在程序后台运行函数(特别是自动保存函数)?QT / C++
- Qt - 在单独的线程中运行函数
- 如何在单独的线程上运行函数(如果线程可用)
- 如何发出编译和运行C++函数的 LLVM IR