[LAU] Simple, easy multithreaded circular buffer library for Linux?

Kjetil S. Matheussen k.s.matheussen at notam02.no
Tue Oct 14 12:39:01 EDT 2008


Olivier Guilyardi:
>
> Ken Restivo wrote:
>> It has been over 7 years since I last messed around with writing Pthreads applications.
>>
>> I recall it as a painful, ugly, brain-numbing task. I located an exercise I did back then to address the consumer/producer problem in Pthreads, and just the sight of it is giving me a headache.
>>
>> I'm being lazy, so instead of researching everything that's out there, I'll ask here: can anyone recommend a relatively simple and painless abstraction library (GPL or LGPL of course) that will give me functions to create a thread in which I can stuff things into a ring buffer, and another thread in which I can pull stuff out of it?
>>
>> By the way, I know that JACK has a very nice event buffer which is insanely easy to use (and I have), and makes multithreading almost transparent, but this isn't a JACK app.
>
> I don't know of any abstraction library, but creating/terminating a normal
> thread with pthread is really an easy task IMO. It's about 10 lines in C.
>
> For inter-thread communication there's Portaudio's ring buffer:
> http://portaudio.com/trac/browser/portaudio/trunk/src/common/pa_ringbuffer.h
>
> It can easily be used out of Portaudio (I'm currently doing that), and it
> features memory barriers [1] which AFAIK Jack's ringbuffer doesn't.
>
> One problem with everything Portaudio is this heavy naming scheme. For a simpler
> API, you might like my little wrapper:
> http://jackbeat.samalyse.org/browser/jackbeat/trunk/src/core/ringbuffer.h
>

Nice. It's probably quicker to copy the jack_ringbuffer.c file out of jack 
though.



> Portaudio actually also offers a callback mechanism (with hidden thread
> creation), so if you're coding an non-JACK audio app, you might want to check it
> out.
>
> For thread synchronization, semaphores (man semaphore.h) are really easy to use.
> However, if you need a lock-free equivalent (for realtime, ...) phtread mutex
> and especially pthread_mutex_trylock are your friends.
>

Those friends can be really cranky sometimes though.
By using atomic operations instead, it's possible to avoid
a lot of headache by not having to synchronize at all.
Performance might be better too. Midishare has lockfree
atomic functions for lifo and fifi queues:
http://midishare.cvs.sourceforge.net/viewvc/midishare/midishare/src/common/atomic/



More information about the Linux-audio-user mailing list