What is the best way to coordinate the accepts of a listenning socket between multiple processes?
I am thinking of one of the two ways:
Have a "Master" process that will send a message to each process when it is its turn to start accepting connections.
So the sequence will be:
Master process gives token to Worker A. Worker A accepts connection, gives token back to Master process. Master process gives token to Worker B. etc.
Each process will have an accepting thread that will be spinning around a shared mutex. Lock the mutex, accept a connection, free the lock.
Any better ideas?
- When a connection comes in ALL processes get woken up. Before accepting the connection the they try to lock a shared mutex. The one to lock the mutex first gets to accept the connection.