简体   繁体   English

TBB:如何获得当前的任务竞技场?

[英]TBB: How to get the current task arena?

I have a small scheduler observer class我有一个小的调度器观察者类

namespace
{
    class TestObserver : public tbb::task_scheduler_observer
    {
    public:
        TestObserver(tbb::task_arena& a) : tbb::task_scheduler_observer(a)
        {
            observe(true); // activate the observer
        }

        /*override*/ void on_scheduler_entry(bool worker)
        {
            // Do something here
            std::cout << "on_scheduler_entry: " << tbb::task_arena::current_thread_index() << std::endl;
        }

        /*override*/ void on_scheduler_exit(bool worker) 
        { 
            std::cout << "on_scheduler_exit: " << tbb::task_arena::current_thread_index() << std::endl;
        }
    };
}

And I'd like to initialize it with the current task arena.我想用当前的任务领域初始化它。 In my main code, I initialize TBB thusly:在我的主代码中,我这样初始化 TBB:

unsigned int numThreads = num_threads;
if (numThreads < 1) numThreads = tbb::task_scheduler_init::automatic;

tbb::task_scheduler_init init(numThreads);

TestObserver obs(...); // <-- fail!

I'd like to initialize the observer with the current task arena.我想用当前的任务领域初始化观察者。 While I don't explicitly initialize one, TBB should do do automatically, right?虽然我没有明确初始化一个,但 TBB 应该自动执行,对吗?

Do not use tbb::task_scheduler_init class;不要使用tbb::task_scheduler_init类; it's already deprecated and will eventually be removed.它已经被弃用,最终将被删除。 If you need to limit the number of threads globally, use tbb::global_control instead;如果需要全局限制线程数,请改用tbb::global_control if you want to limit concurrency for a given job, use tbb::task_arena .如果要限制给定作业的并发性,请使用tbb::task_arena In the latter case, you will also have no problem with attaching an observer to it.在后一种情况下,将观察者附加到它也没有问题。

To create an observer for the current task arena, use tbb::task_scheduler_observer(true) .要为当前任务领域创建观察者,请使用tbb::task_scheduler_observer(true) The boolean argument is used to distinguish such "local" observer from a "global" one, ie not tied to any specific arena (the "global" semantics was the initial one for TBB observers, and it has taken the default constructor).布尔参数用于区分这种“本地”观察者和“全局”观察者,即不绑定到任何特定领域(“全局”语义是 TBB 观察者的初始语义,它采用了默认构造函数)。

You might also create a task_arena object attached to the current arena, and use that for initializing your observer as well as for job submission.您还可以创建一个附加到当前 arena 的task_arena对象,并将其用于初始化您的观察者以及提交作业。 For that, construct or initialize the arena with the special argument: tbb::task_arena(tbb::task_arena::attach()) .为此,使用特殊参数构造或初始化 arena: tbb::task_arena(tbb::task_arena::attach())

If you have not constructed an explicit tbb::task_arena , there is no mechanism I am aware of to retrieve a reference to it, and the default constructor of tbb::task_scheduler_observer will be internally mapped to the arena that is created implicitly.如果您尚未构建显式tbb::task_arena ,则我知道没有机制可以检索对它的引用,并且tbb::task_scheduler_observer的默认构造tbb::task_scheduler_observer将在内部映射到隐式创建的 arena。 The constructor which takes a reference to a tbb::task_arena is intended for use with user-created arenas.引用tbb::task_arena函数旨在与用户创建的tbb::task_arena一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM