PPL - 如何配置本机线程数? [英] PPL - How to configure the number of native threads?

查看:26
本文介绍了PPL - 如何配置本机线程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其 Scheduler 类来管理 PPL 中的本机线程数,这是我的代码:

I am trying to manage the count of native threads in PPL by using its Scheduler class, here is my code:

for (int i = 0; i < 2000; i ++)
{
    // configure concurrency count 16 to 32.
    concurrency::SchedulerPolicy policy = concurrency::SchedulerPolicy(2, concurrency::MinConcurrency, 16, 
            concurrency::MaxConcurrency, 32);
    concurrency::Scheduler *pScheduler = concurrency::Scheduler::Create(policy);
    HANDLE hShutdownEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    pScheduler->RegisterShutdownEvent(hShutdownEvent);
    pScheduler->Attach();

    //////////////////////////////////////////////////////////////////////////

    //for (int i = 0; i < 2000; i ++)
    {
        concurrency::create_task([]{
            concurrency::wait(1000);
            OutputDebugString(L"Task Completed\n");
        });
    }

    //////////////////////////////////////////////////////////////////////////

    concurrency::CurrentScheduler::Detach();
    pScheduler->Release();
    WaitForSingleObject(hShutdownEvent, INFINITE);
    CloseHandle(hShutdownEvent);
}

SchedulerPolicy 的用法来自 MSDN,但它根本不起作用.我上面代码的预期结果是,PPL 会启动 16 到 32 个线程来执行 2000 个任务,但事实是:

The usage of SchedulerPolicy is from MSDN, but it didn't work at all. The expected result of my code above is, PPL will launch 16 to 32 threads to execute the 2000 tasks, but the fact is:

通过观察控制台输出的速度,一秒内只处理了一个任务.我还尝试注释外部 for 循环并取消注释内部 for 循环,但是,这将导致创建 300 个线程,仍然不正确.如果我等待更长的时间,创建的线程会更多.

By observing the speed of console output, only one task was processed within a second. I also tried to comment the outter for loop and uncomment the inner for loop, however, this will cause 300 threads being created, still incorrect. If I wait a longer time, the threads created will be even more.

关于在 PPL 中配置并发的正确方法是什么有任何想法吗?

Any ideas on what is the correct way to configure concurrency in PPL?

推荐答案

已经证明我不应该在任务体内做concurrency::wait,PPL工作在工作窃取模式,当当前任务被wait暂停,它会开始调度队列中剩余的任务,以最大限度地利用计算资源.

It has been proved that I should not do concurrency::wait within the task body, PPL works in work stealing mode, when the current task was suspended by wait, it will start to schedule the rest of tasks in queue to maximize the use of computing resources.

当我在实际项目中使用 concurrency::create_task 时,由于在任务主体中有几个真正的计算,PPL 不会再创建数百个线程.

When I use concurrency::create_task in real project, since there are a couple of real calculations within the task body, PPL won't create hundreds of threads any more.

此外,SchedulePolicy 可用于配置 PPL 可用于处理任务的虚拟处理器数量,这与 PPL 将创建的本机线程数量并不总是相同.

Also, SchedulePolicy can be used to configure the number of virtual processors that PPL may use to process the tasks, which is not always same as the number of native threads PPL will create.

假设我的 CPU 有 8 个虚拟处理器,默认情况下 PPL 只会在池中创建 8 个线程,但是当其中一些线程被 waitlock 挂起时,并且此外,队列中还有更多待处理的任务,PPL 会立即创建更多线程来执行它们(如果虚拟处理器没有完全加载).

Saying my CPU has 8 virtual processors, by default PPL will just create 8 threads in pool, but when some of those threads were suspended by wait or lock, and also there are more tasks pending in the queue, PPL will immediately create more threads to execute them (if the virtual processors were not fully loaded).

这篇关于PPL - 如何配置本机线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆