Re: Threading Question
Re: Threading Question
- Subject: Re: Threading Question
- From: Robert Bielik <email@hidden>
- Date: Tue, 31 Jan 2012 10:05:45 +0100
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