ActorKit.framework: Difference between revisions

From iPhone Development Wiki
(adding language option)
No edit summary
Line 1: Line 1:
{{infobox Framework
The NSAutoreleasePool class is a thin wrapper around the '''NSPushAutoreleasePool''' and '''NSPopAutoreleasePool''' functions.
| vis = Private
| since = 3.0
| classID = AK
}}
<small>'''Languages: English &bull; [[ActorKit.framework/fr|français]]'''</small>
 
'''ActorKit''' is Apple's Objective-C-based implementation for [http://en.wikipedia.org/wiki/Actor_model Action-oriented programming].
 
It is used in [[DataAccess.framework]], [[Message.framework]] and [[searchd]].
 
== Example usage ==


<source lang="objc">
<source lang="objc">
#import <ActorKit/ActorKit.h>
#ifdef __cplusplus
 
extern "C" {
// You must provide a protocol on the messages that the actor can receive.
#endif
@protocol SlowActor
void *NSPushAutoreleasePool(NSUInteger capacity);
-(oneway void)doWorkWithConditionLock:(NSConditionLock*)lock; // oneway is important here. Without it, calls will be synchronous.
void NSPopAutoreleasePool(void* token);
@end
#ifdef __cplusplus
 
 
@interface SlowActor : AKActor<SlowActor> {
  int actor_id;
}
}
-(id)initWithID:(int)_id;
#endif
-(oneway void)doWorkWithConditionLock:(NSConditionLock*)lock;
</source>
@end


Example:


@implementation SlowActor
<source lang="objc">
-(id)initWithID:(int)_id {
static void MyMethod()
  if ((self = [super init]))
{
    actor_id = _id;
    void *pool = NSPushAutoreleasePool(0);
  return self;
     [[[NSObject alloc] init] autorelease];
}
     NSPopAutoreleasePool(pool);
-(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;
}
}
</source>
</source>


== Reference ==
The "capacity" argument of NSPushAutoreleasePool only serves as a hint. It is unused in the current implementation.
* http://github.com/kennytm/iphone-private-frameworks/tree/master/ActorKit/
 
 


{{Navbox Frameworks}}
{{occlass|library=Foundation.framework}}
[[Category:Frameworks/Private]]

Revision as of 09:39, 2 February 2017

The NSAutoreleasePool class is a thin wrapper around the NSPushAutoreleasePool and NSPopAutoreleasePool functions.

#ifdef __cplusplus
extern "C" {
#endif
void *NSPushAutoreleasePool(NSUInteger capacity);
void NSPopAutoreleasePool(void* token);
#ifdef __cplusplus
}
#endif

Example:

static void MyMethod()
{
    void *pool = NSPushAutoreleasePool(0);
    [[[NSObject alloc] init] autorelease];
    NSPopAutoreleasePool(pool);
}

The "capacity" argument of NSPushAutoreleasePool only serves as a hint. It is unused in the current implementation.