home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / MultiThreadedDO_main.m < prev    next >
Encoding:
Text File  |  1996-04-17  |  3.3 KB  |  101 lines

  1. /*
  2.  * You may freely copy, distribute and reuse the code in this example.
  3.  * NeXT Software, Inc. disclaims any warranty of any kind, expressed or implied,
  4.  * as to its fitness for any particular use.  This disclaimer applies to all
  5.  * source files in this example.
  6.  */
  7.  
  8. /* Multithreaded DO example
  9.  * Usage: no arguments, acts as server
  10.  *    any argument, acts as a client (as many as you want)
  11.  * The server and each client all spin off several threads to do their business.
  12.  */
  13.  
  14. #import <Foundation/Foundation.h>
  15. #import "ThreadSafeQueue.h"
  16.  
  17. int ClientCounter = 1000;
  18. id ClientProxy;
  19. NSConditionLock *CLock;
  20.  
  21. @interface ThreadSafeQueue (Helper)
  22. + (void)hammer;
  23. @end
  24.  
  25. @implementation ThreadSafeQueue (Helper)
  26. + (void)hammer {
  27.    unsigned condition;
  28.  
  29.    while (ClientCounter-- > 0) {
  30.         NSAutoreleasePool *pool = [NSAutoreleasePool new];
  31.         id item = [ClientProxy headItem];
  32.  
  33.         if (item) [ClientProxy appendItem:item];
  34.         [pool release];
  35.    }
  36.    // synchronize with main thread
  37.    [CLock lock];
  38.    condition = [CLock condition];
  39.    [CLock unlockWithCondition:++condition];
  40. }
  41. @end
  42.  
  43. int main (int argc, const char *argv[]) {
  44.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  45.     NSArray *arguments = [[NSProcessInfo processInfo] arguments];
  46.     unsigned int limit = [arguments count];
  47.  
  48.     if (limit == 1) {
  49.         // server
  50.         ThreadSafeQueue *queue = [ThreadSafeQueue instance];
  51.         NSConnection *conn = [NSConnection defaultConnection];
  52.         [conn setRootObject:queue];
  53.         [conn registerName:@"Queue"];
  54.  
  55.         [queue appendItem:@"string1"];
  56.         [queue appendItem:@"string2"];
  57.         [queue appendItem:@"string3"];
  58.         [queue appendItem:@"string4"];
  59.         [queue appendItem:@"string5"];
  60.         [queue appendItem:@"string6"];
  61.         [conn runInNewThread];
  62.         [conn runInNewThread];
  63.         [conn runInNewThread];
  64.         [conn runInNewThread];
  65.         [conn runInNewThread];
  66.         [conn runInNewThread];
  67.         NSLog(@"server running in a few threads");
  68.         [[NSRunLoop currentRunLoop] run];
  69.     }
  70.     else {
  71.         // client
  72.         NSConnection *conn = [NSConnection connectionWithRegisteredName:@"Queue" host:nil];
  73.         unsigned nThreads = 6, countThreads;
  74.  
  75.         if (!conn) {
  76.             NSLog(@"didn't connect");
  77.             exit(1);
  78.         }
  79.         CLock = [[NSConditionLock alloc] init];
  80.  
  81.         // enableMultipleThreads allows each of these client threads to use ClientProxy
  82.         // and implicitly add their runLoop to the connection.  Alternatively, we could
  83.         // have, in each thread, explicitly done
  84.         //    [[ClientProxy connectionForProxy] addRunLoop:[NSRunLoop currentRunLoop]];
  85.         [conn enableMultipleThreads];
  86.  
  87.         ClientProxy = [conn rootProxy];
  88.         for (countThreads = 0; countThreads < nThreads; ++countThreads)
  89.             [NSThread detachNewThreadSelector:@selector(hammer) toTarget:[ThreadSafeQueue self] withObject:nil];
  90.  
  91.         // synchronize with worker threads so that we don't lose an item
  92.         [CLock lockWhenCondition:nThreads];
  93.         
  94.         NSLog(@"done hammering, strings should be out of order:");
  95.         NSLog(@"%@", [ClientProxy snapshot]);
  96.     }
  97.     [pool release];
  98.     exit(0);       // insure the process exit status is 0
  99.     return 0;      // ...and make main fit the ANSI spec.
  100. }
  101.