Thread queues for dummies

后端 未结 8 1529
终归单人心
终归单人心 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:14

    I would use ThreadPool.

    Before you start running your jobs, create a ManualResetEvent and an int counter. Add each job to the ThreadPool, incrementing the counter each time.

    At the end of each job, decrement the counter and when it hits zero, call Set() on the event.

    In your main thread, call WaitOne() to wait for all of the jobs to be completed.

提交回复
热议问题