简体   繁体   English

线程池理解问题

[英]Thread pool understanding problem

I'm trying to figure out a way how to align tasks in a thread pool. 我试图找出一种方法来对齐线程池中的任务。 I have 4 ( parallel ) threads and at the beginning ( of each thread execution step ) 10 tasks. 我有4个(并行)线程,在(每个线程执行步骤的开始)有10个任务。 My first attempt was to measure the time of each task, and based on that time find the best combination of tasks in threads to get the best possible result. 我的第一个尝试是测量每个任务的时间,然后根据该时间找到线程中任务的最佳组合,以获得最佳结果。 I'm attempting to write a parallel game engine based on this article http://software.intel.com/en-us/articles/designing-the-framework-of-a-parallel-game-engine/ 我试图根据本文http://software.intel.com/zh-CN/articles/designing-the-framework-of-a-parallel-game-engine/编写一个并行游戏引擎

The problem is that my 'solution' does not work. 问题是我的“解决方案”不起作用。 Are there any more ways to align tasks? 还有其他对齐任务的方法吗?

(The project is in c++) (该项目使用c ++)

To align tasks in parallel threads, use semaphores, events, mutexes. 要在并行线程中对齐任务,请使用信号量,事件,互斥量。 Do not measure the time a task takes. 不要测量任务花费的时间。 Threads are executed at most randomly. 线程最多随机执行。 If you're executing 4 tasks in parallel threads, the first 2 may finish even before the second 2 begin. 如果您在并行线程中执行4个任务,则前两个任务甚至可能在第二个任务开始之前完成。 Here is how to properly do it 这是正确的做法

void Thread1()
{
    task1();
    semaphore1.Release()
}

void Thread2()
{
    task2();
    semaphore1.WaitOne();
    task3();
}

this way, task3 will be always executed after task1 finishes 这样,task3将始终在task1完成后执行

您应该从intels视觉计算站点上检查一下Smoke(他们的多线程处理技术演示引擎)和nullstien,它们都可以处理线程池任务(nullstien有两个变体,一个是基于tbb的调度程序,另一个是定制构建的)

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

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