Re: Threading Question
Re: Threading Question
- Subject: Re: Threading Question
- From: Morgan Packard <email@hidden>
- Date: Tue, 31 Jan 2012 09:00:28 -0500
Yeah, this is more or less what I do. Thanks Robert.
-Morgan
On Tue, Jan 31, 2012 at 4:05 AM, Robert Bielik
<email@hidden> wrote:
ben kamen skrev 2012-01-31 03:09:
Morgan, if you are willing to share a simple example of what that looks like, I would be grateful. What exactly goes into the queue? Function pointers?
One simple SWSR implementation (single writer-single reader) would be to have a Message class:
class Message {
public:
Message() {;}
enum Type {
Type_kAttack,
...
} type;
int param1;
int param2;
...
};
unsigned char fifoQueue[256 * sizeof(Message)] = {0};
unsigned fifoWritePtr = 0;
unsigned fifoReadPtr = 0;
Message* getMessageToWrite()
{
Message* base = (Message*)fifoQueue;
return new(base + (fifoWritePtr & 255)) Message();
}
void postMessageToFifo()
{
++fifoWritePtr;
}
bool fifoHasMessages() {
return (fifoWritePtr != fifoReadPtr);
}
const Message* getMessageFromFifo()
{
const Message* base = (const Message*)fifoQueue;
return base + (fifoReadPtr & 255);
}
void fifoAdvanceRead() {
++fifoReadPtr;
}
To post messages to the fifo:
Message* pMessage = getMessageToWrite();
pMessage->type = Message::Type_kAttack;
pMessage->param1 = 42;
postMessageToFifo();
Reading/processing messages in render callback:
while(fifoHasMessages()) {
const Message *pMessage = getMessageFromFifo();
// TODO: Process the message
// Next message...
fifoAdvanceRead();
}
That should roughly get you started.
Regards,
/Rob
______________________________
_________________
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
--
===============
Morgan Packard
cell: (720) 891-0122
aim: mpackardatwork