Optimal multithread model

common at gmx.ch common at gmx.ch
Mon Mar 15 15:52:18 CET 2010


I have something similar, therefore "how I did it".

All polling is done by libev in the main-loop, a single main loop.
If a connection is established and processing the data needs some time, 
I remove the watchers from the loop, and creates a task from the data to 
process and the function to process the data, and pushes this set to an 
glib thread pool.

The thread is allowed calls the function to process the data with the 
data as argument, once it is done, it creates its result set, the data 
and a function which will deal with the result-data, and pushes this set 
to an async queue.
To signal libev there is something in the async queue, I use ev_async 
from the thread.

libev catches the ev_async, I remove all pending sets from the async 
queue, call the provided function with the data provided.
The function provided can rearm the watchers for the loop, and continue 
in the main loop.

It's rather simple, but works as expected.


Markus



More information about the libev mailing list