Re: Threading Question
Re: Threading Question
- Subject: Re: Threading Question
- From: Mark Pauley <email@hidden>
- Date: Tue, 31 Jan 2012 16:50:49 -0800
This is exactly what I'm talking about when I speak of the functions available for you in <libkern/OSAtomic.h>
For example: OSAtomicEnqueue and OSAtomicDequeue actually provide the non-blocking FIFO behavior described by previous responses.
From the Man Page:
> The routines OSAtomicEnqueue() and OSAtomicDequeue() operate on singly linked LIFO queues. Ie, a dequeue operation will return the most recently enqueued element, or NULL if the list is empty.
> The operations are lockless, and barriers are used as necessary to permit thread-safe access to the queue element. offset is the offset in bytes to the link field in the queue element. For
> example:
>
> typedef struct elem {
> long data1;
> struct elem *link;
> int data2;
> } elem_t;
>
> elem_t fred, mary, *p;
>
> OSQueueHead q = OS_ATOMIC_QUEUE_INIT;
>
> OSAtomicEnqueue( &q, &fred, offsetof(elem_t,link) );
> OSAtomicEnqueue( &q, &mary, offsetof(elem_t,link) );
>
> p = OSAtomicDequeue( &q, offsetof(elem_t,link) );
>
> In this example, the call of OSAtomicDequeue() will return a ptr to mary.
This uses an LIFO, which while it may cause problems (if your producers keep pace in lockstep with your consumers you will get starvation and out-of-order messages), probably will be just fine for you (since you probably drain the messages MUCH faster than you enqueue them on average). I would just use this API unless you can prove that the LIFO isn't cutting it.
again: RTEM (Read the Excellent Manpage) "man atomic" for further text and API to help you.
_Mark
On Jan 31, 2012, at 2:00 PM, email@hidden wrote:
> <pedantic>
>
> Robert Bielik wrote:
> [...]
>>
>> void postMessageToFifo()
>> {
>
> (write barrier needed here)
>
>> ++fifoWritePtr;
>> }
>>
> [...]
>>
>> const Message* getMessageFromFifo()
>> {
>
> (read barrier needed here)
>
>> const Message* base = (const Message*)fifoQueue;
>> return base + (fifoReadPtr & 255);
>> }
>>
>
> </pedantic>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Coreaudio-api mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden