Ensuring task execution order in threadpool

后端 未结 17 951
情深已故
情深已故 2020-12-12 11:54

I have been reading about the thread-pool pattern and I can\'t seem to find the usual solution for the following problem.

I sometimes want tasks to be executed serial

17条回答
  •  温柔的废话
    2020-12-12 12:38

    To do what you want to do with a threadpool, you might have to create some kind of scheduler.

    Something like that:

    TaskQueue -> Scheduler -> Queue -> ThreadPool

    Scheduler runs in its own thread, keeping tracks of dependencies between jobs. When a job is ready to be done, the scheduler just pushes it in the queue for the threadpool.

    The ThreadPool might have to send signals to the Scheduler to indicate when a job is done so the scheduler can put jobs depending on that job into the Queue.

    In your case, the dependencies could probably be stored in a linked list.

    Let's say you have the following dependencies: 3 -> 4 -> 6 -> 8

    Job 3 is running on the threadpool, you still have no ideas that job 8 exists.

    Job 3 ends. You remove the 3 from the linked list, you put job 4 on the queue to the threadpool.

    Job 8 arrives. You put it at the end of the linked list.

    The only constructs that have to be fully synchronized are the Queues before and after the scheduler.

提交回复
热议问题