While programming parallel programs a lot of time I encounter with problem of making something with unique group of 2 values. In other words this is handshaking problem where each man has to shake his hand with everyone else's. After handshaking there is a dinner waiting for all participants.
There are 2 approaches I'm aware of how to do this:
Number all men and make it parallel for each man, who will shake hands with men that have lower number. Dinner will be cold before man with last number asks everyone to shake his hand.
We will tell everyone how much participants there are and create condition based on that value, so everyone will shake aprox. equal number of hands in parallel. Dinner will be cold because participants are bad at math. (In this case people can act like someone else)
We can also exchange men for numbers and shaking for comparing, multiplying, etc. The problem is that we want eat that dinner before it is cold and waiting for one thread or making lot of conditions will slow the process down.
Is there other more efficient way to do this?