Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198
ActorKit.framework: Difference between revisions - iPhone Development Wiki

ActorKit.framework: Difference between revisions

From iPhone Development Wiki
(ActorKit)
 
No edit summary
Line 1: Line 1:
{{infobox Framework
{{infobox Framework
| vis = ActorKit
| vis = Private
| since = 3.0
| since = 3.0
| classID = AK
| classID = AK
Line 80: Line 80:


{{Navbox Frameworks}}
{{Navbox Frameworks}}
[[Category:Frameworks/Private]]

Revision as of 16:41, 21 September 2009

ActorKit.framework
Private Framework
Availabile 3.0 – present
Class Prefix AK
Headers [headers.cynder.me]


ActorKit is Apple's Objective-C-based implementation for Action-oriented programming [1].

It is used in DataAccess.framework, Message.framework and searchd.

Example usage

#import <ActorKit/ActorKit.h>

// You must provide a protocol on the messages that the actor can receive.
@protocol SlowActor
-(oneway void)doWorkWithConditionLock:(NSConditionLock*)lock;  // oneway is important here. Without it, calls will be synchronous.
@end


@interface SlowActor : AKActor<SlowActor> {
  int actor_id;
}
-(id)initWithID:(int)_id;
-(oneway void)doWorkWithConditionLock:(NSConditionLock*)lock;
@end


@implementation SlowActor
-(id)initWithID:(int)_id {
  if ((self = [super init]))
    actor_id = _id;
  return self;
}
-(oneway void)doWorkWithConditionLock:(NSConditionLock*)lock; {
  printf("Worker %d is doing work...\n", actor_id);
  usleep(actor_id*actor_id*100000);
  [lock lock];
  [lock unlockWithCondition:[lock condition]+1];
  printf("Worker %d has done.\n", actor_id);
}
@end



int main () {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  
  SlowActor* actors[10];
  for (unsigned i = 0; i < 10; ++ i)  {
    actors[i] = [[[SlowActor alloc] initWithID:i] autorelease];
    // You must call -startThreadDispatchQueue before sending any works to the actors.
    [actors[i] startThreadDispatchQueue];
  }
    
  NSConditionLock* lock = [[NSConditionLock alloc] initWithCondition:0];
  
  for (unsigned i = 0; i < 10; ++ i)
    // Use -send to obtain the actor's mailbox. Send messages to their mailboxes to allow asynchronous messages.
    [[actors[i] send] doWorkWithConditionLock:lock];
  
  printf("Waiting for all workers...\n");
  
  [lock lockWhenCondition:10];
  [lock unlock];
  
  printf("All workers done...\n");

  [pool drain];
  
  return 0;
}

Reference