Permalink
Browse files

updated readme

  • Loading branch information...
1 parent 84f39a8 commit 0da6c31a4343fa4a6e99cb0413cff1ada84a03d6 @stevedekorte committed Sep 13, 2011
Showing with 15 additions and 12 deletions.
  1. +15 −12 README.txt
View
@@ -1,18 +1,21 @@
-ActorKit extends NSObject to allow all objects to become coroutine based actors where an actor has a cooperative thread and a queue of incoming messages which it processes in first-in-first-out order. A message to the actor can also return a "future" object which only blocks when the result is requested and is not yet ready, automatically minizing blocking time or busy waits. Futures also support automatic deadlock avoidance by checking for deadlock loops when a result is requested.
+ActorKit extends NSObject to allow all objects to become actors. An actor has an os thread and a queue of incoming messages which it processes in first-in-first-out order. A message to the actor can also return a "future" object which only blocks when the result is requested and is not yet ready and this is done without busy waits. Futures also support automatic deadlock avoidance by checking for deadlock loops when a result is requested.
-Simple example of using a future:
+example:
- // this returns immediately
+ // look ma, no state machine or callbacks
+
+ Future *f1 = [[NSURL URLWithString:@"http://yahoo.com"] futurePerformSelector:@selector(fetch:) withObject:nil];
+ Future *f2 = [[NSURL URLWithString:@"http://google.com"] futurePerformSelector:@selector(fetch:) withObject:nil];
+
+ NSLog(@"request 1 returned %i bytes", (int)[(NSData *)[f1 result] length]);
+ NSLog(@"request 2 returned %i bytes", (int)[(NSData *)[f2 result] length]);
- Future *future = [NSURLConnection futurePerformSelector:@selector(sendRequest:) withObject:request];
- // ...later, when we need the result, [future result] blocks if the result is not yet ready
-
- NSDictionary *result = [future result];
- NSData *data = [result objectForKey:@"data"];
-
-Note that if you did this from the main thread, only made one request, and asked for the result right away, it would no different than doing a normal synchronous NSURLConnection request. To get more concurrency, we need to perform our requests from another actor's thread, so the main thread can return. While there are active actors, the main thread will have a timer that periodically calls back in and allows all the active actors to have a chance to process their next message.
-
-See the [forthcoming] text code for an example of how to do that.
+note:
+ // you'll beed to add this method because dataWithContentsOfURL: is a class method but actors have to be instances
+
+ @implementation NSURL (fetch)
+ - (NSData *)fetch:sender { return [NSData dataWithContentsOfURL:self]; }
+ @end

0 comments on commit 0da6c31

Please sign in to comment.