empty loop, adding watchers, one minute idle

Marc Lehmann schmorp at schmorp.de
Tue Mar 20 14:11:22 CET 2012

On Mon, Mar 19, 2012 at 09:58:24PM +0100, Zoltán Lajos Kis <zoltan.lajos.kis at gmail.com> wrote:
> create an io watcher for the socket, and start the socket on the started
> loop. (note: the request processing, and watcher preparation is done on
> another thread.)
> get no immediate results. However, roughly after one minute (since starting
> the loop?) the events show up as they were buffered, and from that time
> packets are

since you use threads, are you sure you use proper locking? from the
symptoms, it seems you don't - in geenral, when you access a shared
resource (such as the event loop) you need to lock it with e.g. a mutex.

> What I do now is that I set up an async watcher and a message queue to the
> thread. Then instead of starting the watcher, I put the watcher ('s
> pointer) to the queue, and notify the loop about this using the async
> watcher.

Yes - but if you don't use proper locking, you will get data corruption
under certain circumstances. If you want to access the same loop from
different threads, you need to lock it against concurrent access - see
thwe thread locking example in the manual for example.

> My question is whether someone could explain me what is actually going on,
> and what would be a cleaner solution for the problem.

1. read the documentation, if you then sitll have questions, feel free to
   ask them.
2. thread programming is notoriously difficult - as a rule of thumb,
   protecting shared resources with a mutex is a must, but there are many other
   cases where you need to use special thread functions when working with
   threads, such as using condvars, or async watchers.

failure to lock will most likely seem to work under light load, but will
lead to memory corruption unless you are extremely lucky. it will also
cause the symptom you describe, as if you would properly lock the loop you
would block in the lock (because the other thread is inside ev_run).

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

More information about the libev mailing list