Using libev in a GUI

Marc Lehmann schmorp at schmorp.de
Thu Jan 7 07:52:47 CET 2010


On Wed, Jan 06, 2010 at 10:21:14AM -0800, Tim McClarren <tim at idle-games.com> wrote:
> Actually, I have code that starts and stops watchers on the event loop  
> associated with thread A, from thread B.  I use ev_asyncs to poke thread  
> A to let him know that watcher state was changed.  This seems to work.  

Well, as with every race condition, you might have luck, and usually, some
of your users will lose their data due to corruption :)

> Is it not designed to work?

No, it corrupts internal data structures when your threads happen to run
in parallel or get scheduled while in libev code.

The only correct way to do that is to ensure that only one thread ever
accesses the loop, e.g. by using mutexes.

> calls to change watcher state over to be inside the async callback, but  
> then I'd have to either create one async callback per watcher or attach  
> state to one of the passed in data structures.

Or use a queue, or locking.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      pcg at goof.com
      -=====/_/_//_/\_,_/ /_/\_\



More information about the libev mailing list