gpt4 book ai didi

c++ - 英特尔 TBB 在并行线程中运行函数?

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:07 24 4
gpt4 key购买 nike

基本上我正在开发一个 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()

最佳答案

英特尔 TBB 并非旨在用于此类目的。引自 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.

你想做的事情可以通过 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++ - 英特尔 TBB 在并行线程中运行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11563502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com