Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: f0c317e647
Fetching contributors…

Cannot retrieve contributors at this time

39 lines (24 sloc) 1.331 kB
ActorKit extends NSObject to allow all objects to become actors.
Each actor has an os thread and a queue of incoming messages which it processes in
first-in-first-out order.
Any message to an actor returns a "future" object which only blocks when it is accessed.
Futures detect and raise an exception in situations that would cause a deadlock.
// look ma, no state machines or callbacks
// these spawn threads to and return immediately
NSData *future1 = [(NSURL *)[[NSURL URLWithString:@""] asActor] fetch];
NSData *future2 = [(NSURL *)[[NSURL URLWithString:@""] asActor] fetch];
// now when we try to access the values, they block if the values aren't ready
NSLog(@"request 1 returned %i bytes", (int)[future1 length]);
NSLog(@"request 2 returned %i bytes", (int)[future2 length]);
// We just did a coordinated interaction between three threads without
// by only adding two tokens.
// you'll need to add this method for the above example because
// dataWithContentsOfURL: is a class method but actors have to be instances
@implementation NSURL (fetch)
- (NSData *)fetch:sender { return [NSData dataWithContentsOfURL:self]; }
Thanks to Mark Papadakis for help with figuring out how to properly use mutex conditions.
Jump to Line
Something went wrong with that request. Please try again.