Ensuring task execution order in threadpool

后端 未结 17 942
情深已故
情深已故 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:42

    I think you're mixing concepts. Threadpool is ok when you want to distribute some work among threads but if you start mixing dependencies between threads then it isn't such a good idea.

    My advice, simply don't use the threadpool for those tasks. Just create a dedicated thread and keep a simple queue of sequential items that must be processed by that thread alone. Then you can keep pushing tasks to the thread pool when you don't have a sequential requirement and use the dedicated thread when you have.

    A clarification: Using common sense, a queue of serial tasks shall be executed by a single thread processing each task one after another :)

提交回复
热议问题