Optimal multithread model

Christophe Meessen christophe at meessen.net
Mon Mar 15 13:55:18 CET 2010


I finally found out how to implement the desired multi thread model by 
using the existing API. One needs to use the ev_loop( ONE_SHOT) call.

The following code skeleton shows how I would do it.

worker_thread_function(...)
{
      while( !done )
      {
            // waiting to call ev_loop
            waiting_thread_count++;
            while( ev_loop_in_progress )
                 wait_notification();  // done with a condition variable
            waiting_thread_count--;

            // call the ev_loop
            if( !done )
            {
                ev_loop_in_progress  = true;
                ev_loop( ONE_SHOT );
                ev_loop_in_progress = false;
            }
            notify();

            // process pending requests
            while( has_pending_requests() &&  waiting_thread_count > 0 )
                 process_request();
     }
}

One of the callbacks would eventually set the done flag to false. The 
callback handling incoming data event would collect data until a 
complete request is obtained and then queues it for processing. This 
would do the job nicely, though the overhead of the repeated call of 
ev_loop is unknown.

There must be at least two worker threads otherwise it will simply queue 
request. This is because it is designed to have always one worker thread 
executing the ev_loop.

The code can be adapted to automatically increase the number of threads 
(remember to add an upper limit !) if there is a pending request and no 
waiting thread. A worker thread would then stop if it executed ev_loop 
beyond a time limit without processing any requests, unless the waiting 
thread count is less than 2.

--
Ch. Meessen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3277 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20100315/1dcaad52/attachment.bin>


More information about the libev mailing list