When you want to coordinate nodes, PAIR sockets won’t work well any more. This is one of the few areas where the strategies for threads and nodes are different. Principally, nodes come and go whereas threads are static. PAIR sockets do not automatically reconnect if the remote node goes away and comes back.
The second significant difference between threads and nodes is that you typically have a fixed number of threads but a more variable number of nodes. Let’s take one of our earlier scenarios (the weather server and clients) and use node coordination to ensure that subscribers don’t lose data when starting up.
This is how the application will work:
The publisher knows in advance how many subscribers it expects. This is just a magic number it gets from somewhere.
The publisher starts up and waits for all subscribers to connect. This is the node coordination part. Each subscriber subscribes and then tells the publisher it’s ready via another socket.
When the publisher has all subscribers connected, it starts to publish data.
Example 2-13. Synchronized publisher (syncpub.c)
// Synchronized publisher
// We wait for 10 subscribers
#define SUBSCRIBERS_EXPECTED 10
// Socket to talk to clients