CocoaDev

Edit AllPages

This is an example of use for an NSConditionLock, that came from discussion ExitMainThreadWithAnEmptyWhileLoop.

The class MultiJob starts with an array of jobs, where a job is simply some calculation one can start with [job runJob], each job being completely independent of the others. To take advantage of multiple processor, these jobs could be run in separate threads. No fancy multithreading here, just the main thread, plus (n-1) secondary threads, all running these jobs one after the other, getting the next job from a common queue.

The number of threads running the jobs is actually stored in the condition of an NSConditionLock named threadLock. Before starting the jobs, the condition is set to the number of threads with the code [threadLock lock]; [threadLock unlockWithCondition:threadCount]; . When a thread is done, it decrements the condition value with a [threadLock lock];[threadLock unlockWithCondition: ([threadLock condition] - 1) ];. Finally, when the main thread is done runnning the jobs, it waits for the other threads to finish with a [threadLock lockWhenCondition:0]; [threadLock unlock];.

Here is the code

int main (int argc, const char * argv[]) { MultiJob jobs; NSArray *someJobs; / … someJobs are defined … */ jobs=[[MultiJob alloc] initWithJobs:someJobs]; [jobs runJobsInMultipleThreads:2]; }

@interface MultiJob : NSObject { NSArray *jobArray; int nextJobIndex; NSLock *nextJobIndexLock; NSConditionLock *threadLock; BOOL forceRun; }

//public

//private

@end

@implementation BCKMultiJob

//manages the job queue by manipulating nextJobIndex inside a lock

//each thread will run that code, including the main thread

@end