Thread queues for dummies

后端 未结 8 1531
终归单人心
终归单人心 2021-02-06 11:59

I have what I assume is a pretty common threading scenario:

  • I have 100 identical jobs to complete
  • All jobs are independent of each other
  • I want t
8条回答
  •  我在风中等你
    2021-02-06 12:04

    Even though the other answers are nice if you want another option (you can never have enough options), then how about this as an idea.

    Just put the data for each job into a structure, which is in a FIFO stack.

    Create 15 threads.

    Each thread will get the next job from the stack, popping it off.

    When a thread finishes the processing, get the next job, if the stack is empty the thread dies or just sleeps, waiting.

    The only complexity, which is pretty simple to resolve, is having the popping be in a critical section (synchronize read/pop).

提交回复
热议问题