Note that there are some explanatory texts on larger screens.

plurals
  1. POEnsuring task execution order in threadpool
    text
    copied!<p>I have been reading about the thread-pool pattern and I can't seem to find the usual solution for the following problem.</p> <p>I sometimes want tasks to be executed serially. For example, I read chunks of text from a file and for some reason I need the chunks to be processed in that order. So basically I want to eliminate concurrency <strong>for some of the tasks</strong>.</p> <p>Consider this scenario where the tasks with <code>*</code> need to be processed in the order they were pushed in. The other tasks can be processed in any order.</p> <pre><code>push task1 push task2 push task3 * push task4 * push task5 push task6 * .... and so on </code></pre> <p>In the context of a thread-pool, without this constraint, a single queue of pending tasks works fine but clearly here it doesn't.</p> <p>I thought about having <em>some of the threads</em> operate on a thread-specific queue and the others on the "global" queue. Then, in order to execute some of the tasks serially, I simply have to push them onto a queue where a single thread looks. It <em>does</em> sounds a bit clumsy.</p> <p>So, the real question in this long story: how would you solve this ? <strong>How would you ensure those tasks are ordered</strong>?</p> <h3>EDIT</h3> <p>As a more general problem, suppose the scenario above becomes</p> <pre><code>push task1 push task2 ** push task3 * push task4 * push task5 push task6 * push task7 ** push task8 * push task9 .... and so on </code></pre> <p>What I mean is that the tasks within a group should be executed sequentially, but the groups themselves can mix. So you can have <code>3-2-5-4-7</code> for example.</p> <p>One other thing to note is that I don't have access to all the tasks in a group upfront (and I can't wait for all of them to arrive before starting the group).</p> <p>Thank you for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload