| ActorKit.framework/fr | |
|---|---|
| Private Framework | |
| Available | 3.0 – present |
| Class Prefix | AK |
Languages: English • français
ActorKit est une implémentation Apple basée sur l'Objective-C pour la programmation orientée objet.
Cela est utilisé dans DataAccess.framework, Message.framework et searchd.
Exemple d'usage
#import <ActorKit/ActorKit.h> // You must provide a protocol on the messages that the actor can receive. @protocol SlowActor -(onewayvoid)doWorkWithConditionLock:(NSConditionLock*)lock;// oneway is important here. Without it, calls will be synchronous. @end @interface SlowActor : AKActor<SlowActor>{ intactor_id; } -(id)initWithID:(int)_id; -(onewayvoid)doWorkWithConditionLock:(NSConditionLock*)lock; @end @implementation SlowActor -(id)initWithID:(int)_id{ if((self=[superinit])) actor_id=_id; returnself; } -(onewayvoid)doWorkWithConditionLock:(NSConditionLock*)lock;{ printf("Worker %d is doing work...\n",actor_id); usleep(actor_id*actor_id*100000); [locklock]; [lockunlockWithCondition:[lockcondition]+1]; printf("Worker %d has done.\n",actor_id); } @end intmain(){ NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init]; SlowActor*actors[10]; for(unsignedi=0;i<10;++i){ actors[i]=[[[SlowActoralloc]initWithID:i]autorelease]; // You must call -startThreadDispatchQueue before sending any works to the actors. [actors[i]startThreadDispatchQueue]; } NSConditionLock*lock=[[NSConditionLockalloc]initWithCondition:0]; for(unsignedi=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"); [locklockWhenCondition:10]; [lockunlock]; printf("All workers done...\n"); [pooldrain]; return0; }
