mailbox is the inter-thread communication mechanism . In 0mq, whenever one thread wants to modify the status of the other one, it sends a message to inform the other thread to do it. You don’t need any synchronization among threads since they don’t share anything.
mailbox is implemented using sock_pair. The data type communicated through the sock_pair is defined as command_t (command.h/cpp). Other than the message type and message parameters, one import component of every message is the “destination”, which is an interferce type defined as “object_t” (object.cpp/h). object_t has a function process_command(command_t& cmd), for different message type, a different virtual function will be called to response the message type.
In current 0mq, every iothread object and every socket own a mailbox.
One important command for 0mq socket is “Activiate”. A thread calling blocking read on an empty pipe will be blocked by calling process_command (block on reading sock_pair); Later on, a writer thread to the pipe will send an “Activate” command to wake it up. The lockless pipe of 0mq adopts an interesting design: The writer can know exactly if the reader is blocked or is going to be blocked, so it only does notification when necessary — This greatly improves the performance.