Example of custom backend with thread?

Brandon Black blblack at gmail.com
Tue Dec 21 22:00:07 CET 2010

On Tue, Dec 21, 2010 at 2:17 PM, AJ ONeal <coolaj86 at gmail.com> wrote:
> I have a function to process data on a DSP which blocks
> and a network socket which gives data to process.

Not knowing the rest of the details, my first stab would probably be
to continue with your model of having a separate thread for handling
the blocking DSP interactions (1 thread per DSP if there's more than
one DSP).  You could implement a work queue that's locked with a
pthread mutex and has a pthread condition variable for signaling
readiness.  Might be interesting to track average workqueue length to
know if the DSP stuff is bottlenecking too.  Very rough pseudocode for
the lock/cond interaction (ignoring many complexities of any real
implementation, threads are tricky, you have to be careful about who
owns what data at any given time):

pthread_mutex_t workqueue_mutex;
pthread_cond_t workqueue_cond;

// main thread, libev callback for client socket data
client_data_recv() {
    // ... put received network data in some buffer ...
    add_buffer_ptr_to_workqueue(mybufptr, workqueue);

// DSP thread
mainloop() {
    while(1) {
        remove_buffer_ptr_from_workqueue(mybufptr, workqueue);
        // ... process work item from mybufptr ...

More information about the libev mailing list