Efficient synchronization of querying an array of resources
- by Erel Segal Halevi
There is a list of N resources, each of them can be queried by at most a single thread at a time.
There are serveral threads that need to do the same thing at approximately the same time: query each of the resources (each thread has a different query), in arbitrary order, and collect the responses.
If each thread loops over the resources in the same order, from 0 to N-1, then they will probably have to wait for each other, which is not efficient.
I thought of letting the threads loop over the resources in a random permutation, but this seems too complex and also not so efficient, for example, for 2 resources and 2 threads, in half the cases they will choose the same order and wait for each other.
Is there a simple and more efficient way to solve this?