How can I write a main loop which blocks while waiting for messages from multiple sources? As I understand it, the preferred way of writing an event-processing loop is to have it block while waiting for events.
However, how can blocking be handled correctly when messages may come from multiple sources?
I would like to write a GTK GUI which responds both to user input events and to messages sent via nanomsg.
GTK allows its events to be handled by calling gtk_main()
or, in a non-blocking way, with gtk_main_iteration_do (FALSE)
.
Nanomsg can receive a message in either blocking or non-blocking mode, as well as poll for messages.
Is it possible to somehow block until whichever source first has input available "unblocks"? I.e. is there an alternative to using sleep
which remains responsive to all events?