问题
I have a tricky question that I'm not sure how to tackle. Lets say we have 10 jobs(N=10) and 4 workers(num_threads=4), I need to find a function that can assign start indexes for each of these workers in such a way that no worker does more or less than 2 jobs than any other worker. As a naive approach I'm using the start_index = rank * N/num_threads formula, but this produces unbalanced results (the 4th thread only has 1 job while the first 3 have 3 jobs). Any nudging in the right direction would be very appreciated!
回答1:
The easy way to do this is to keep a single/global queue of jobs-that-are-ready-to-be-performed. Whenever a worker-thread needs something to do, it should pop the next available job out of the queue (serialize access to the queue with a mutex, of course) and execute that job. That way the load-balancing happens automatically/naturally, regardless of how many workers or jobs there are, and regardless of how much or how little time any particular job takes to execute.
If the worker-thread tries to get a job out of the queue and the queue is empty, you can handle that either by having the worker thread exit (since there are no more jobs to do), or (if you are expecting more jobs to be added to the queue in the future), the worker-thread could then block on a condition-variable that will get signalled after a job has been added to the queue (at which point the worker-thread can try again to get a job from the queue).
来源:https://stackoverflow.com/questions/64899501/multithreading-balancing