Re: Asynchronous timers (without a run loop)
Re: Asynchronous timers (without a run loop)
- Subject: Re: Asynchronous timers (without a run loop)
- From: Joseph Kelly <email@hidden>
- Date: Thu, 04 Dec 2008 08:24:47 -0800
On Dec 4, 2008, at 7:14 AM, Påhl Melin wrote:
I'm very happy with the messaging framework and would like to use it
on Mac OS X as well. Run loops are not compatible with this approach
since my threads need to run forever or block in my framework when
they are waiting for messages (the timers are used to generate timer
messages internally in the framework).
To give you some perspective, that's exactly what Runloops do. They
block a thread indefinitely until a "message" comes in, at which point
the thread unblocks and begins executing some kind of callback
routine. When the callback returns, the thread blocks again until the
next message comes in. Runloops are simply an abstraction that
associates an arbitrary thread with a set (or sets) of triggers which
cause the thread to unblock and do something.
So for instance, to give you a top level overview of how this might
work -- this is a rough C example, but could be adapted to an OOP model:
CFRunLoopRef gWorkerThreadRLoop = NULL; // this could be a global
variable or a member variable of a C++ or ObjC object.
main(...)
{
SpawnAThread(MyThreadRoutine); // you could use pthreads, or
NSThread, or even Boost threads.
while(gWorkerThreadRLoop == NULL); // prbly add a timeout / sleep here.
// init some other stuff
... Run the Main Thread's Runloop...
// Something causes us to wake up, so lets forward a message to the
worker thread:
TriggerAMessage(gWorkerThreadRLoop, msg1);
TriggerAMessage(gWorkerThreadRLoop, msg2);
...
// time to quit
CFRunLoopStop(gWorkerThreadRLoop)
}
MyThreadRoutine(...)
{
gWorkerThreadRLoop = CFRunLoopGetCurrent(); // this will
automatically/implicitly create the runloop associated w/ this thread.
... add the timer and other sources...
CFRunLoopRun();
... remove my sources / timers...
gWorkerThreadRLoop = NULL;
}
MsgDispatcher(incomingMsg) // this gets called on the worker thread in
response to TriggerAMessage
{
// Dispatch the incomingMsg
}
If this looks suitable, I can give you more details about how to
implement.
Joe K.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden