[zeromq-dev] Client API with zactor

Kevin Wang kevin at spikegadgets.com
Fri Jan 19 21:48:52 CET 2018


Hello everyone,

I am building a client API that abstracts away sockets and the Zeromq 
and CZMQ layer. To do that, my class has a zactor containing a zloop, 
polling on all the various sockets in the class.

I eventually want my users to define their own "events", which are 
messages containing an event name (string) and associated data in the 
next frame, and for them to write their own functions that are called by 
the zloop when their messages arrive. I am doing this by creating a 
virtual function for them to override, which they can use to define 
their own behavior.

And everything was good, until I realized that their defined function 
would be called in the zactor thread, not the user's thread. If they 
chose to define their function to change some variable or call some 
other function, that would cause a race condition.

At least, that is what I think could happen. I realize that this is only 
loosely related to zeromq, and more about API design, but is there 
anyone else that has run into this problem? Is this a valid concern?

My proposed solution would be to push these event messages to the user 
thread, and make the user call a processEvents() function when they can, 
which pulls the event messages and acts on them.

Similar to:

while(!quitCondition){

client.processEvents()

//Run other code

}

I am new to dealing with threads and am not sure if this is a common 
pattern, or if there is something basic I am missing.


Thanks

Kevin




More information about the zeromq-dev mailing list