线程构建块上的任务

发布于 2024-09-12 19:28:02 字数 778 浏览 5 评论 0原文

这是示例代码:

#include <iostream>
#include <list>
#include <tbb/task.h>
#include <tbb/task_group.h>
#include <stdlib.h>
#include <boost/thread.hpp>

using namespace tbb;

 long fib(long a)
{
  if (a < 2) return 1;

  return fib(a - 1) + fib(a - 2);
}

class PrintTask 
{
public:
    void operator()()
    {
        std::cout << "hi world!: " <<  boost::this_thread::get_id() << std::endl;

        fib(50);
    }
};

int main(int argc, char** argv)
{     
    task_group group;

    for (int i = 0; i < 100; ++i)
    {
      group.run(PrintTask());
    }      

    group.wait();

    return(0);
}

这里我计算一个大的斐波那契序列只是为了模拟非阻塞计算。我预计这段代码会生成两个以上的线程(我的计算机是 Core2Duo),但只调用第一个和第二个任务。这就是被看好的人?

Here is the example code:

#include <iostream>
#include <list>
#include <tbb/task.h>
#include <tbb/task_group.h>
#include <stdlib.h>
#include <boost/thread.hpp>

using namespace tbb;

 long fib(long a)
{
  if (a < 2) return 1;

  return fib(a - 1) + fib(a - 2);
}

class PrintTask 
{
public:
    void operator()()
    {
        std::cout << "hi world!: " <<  boost::this_thread::get_id() << std::endl;

        fib(50);
    }
};

int main(int argc, char** argv)
{     
    task_group group;

    for (int i = 0; i < 100; ++i)
    {
      group.run(PrintTask());
    }      

    group.wait();

    return(0);
}

Here I'm computing a big fibonacci sequence just to simulate non-blocking computation. I was specting that this code would generate more than two threads (my computer is a Core2Duo), but only the first and second tasks are called. This is the spected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-09-19 19:28:02

是的,这是预期的行为。

TBB 是一个旨在并行化代码以提高性能的库。它不是为异步任务设计的 - 官方文档指出您应该使用另一个库,例如 pthreads,来执行此类任务(或 boost::thread,在您的情况下)。

为了获得最大性能,拥有比内核更多的线程是没有任何意义的,因为涉及到一些显着的开销(不仅是上下文切换,还包括刷新缓存等)。

编辑:
您可以在教程。具体来说,第 1.2 节“好处”中指出

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

英特尔® 线程构建模块与其他线程兼容
包。因为该库并非旨在解决所有线程问题,
它可以与其他线程包无缝共存。

Yes, this is the expected behavior.

TBB is a library designed to parallelize code for PERFORMANCE. It is not designed for asynchronous tasks - the official documentation states that you should use another library, eg pthreads, for such tasks (or boost::thread, in your case).

For maximum performance, it does not make any sense to have more threads than you do cores, as there are some significant overheads involved (not just context switching, but also things like flushing the cache).

EDIT:
You can read about it in the Tutorial. Specifically, in section 1.2 "Benefits" it states

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.

and

Intel® Threading Building Blocks is compatible with other threading
packages. Because the library is not designed to address all threading problems,
it can coexist seamlessly with other threading packages.

雾里花 2024-09-19 19:28:02

大规模多线程阻塞行为(std::cout 使用)是多线程中的反模式,可能会导致不良行为,因为这是错误的做法。此外,TBB 保留随心所欲地实现 group.run() 并生成它喜欢的任何线程的权利。如果你只有双核,并且你的调用工作繁重,为什么它应该产生两个以上的线程呢?操作系统和其他应用程序将很乐意吃掉剩余的后台时间。

Massively multithreading blocking behaviour (std::cout use) is an anti-pattern in multithreading and may result in bad behaviour, because it's the wrong thing to do. In addition, TBB reserves the right to implement group.run() however the hell it likes and spawn whatever threads it likes. If you only have a dual core, and you call with heavy work, why should it spawn more than two threads? The OS and other apps will happily eat remaining background time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文