There are two main ways to control the atomicity of thread synchronization/security.
1: NSCondition object
2: NSLock object
The two locks are used in the same way.
While (TRUE) {[lock]; // [_ ticketCondition lock]; if (numberOfTickets> 0) {[NSThread sleepForTimeInterval: 0.1]; scaleOfTickets ++; numberOfTickets --; NSString * thread = [[NSThread currentThread] name]; NSLog (@ "Current votes % d, sold votes % d, total votes % d, current thread: % @", numberOfTickets, scaleOfTickets, numberOfTickets + scaleOfTickets, thread);} else {[self. buttonStart setHidden: NO]; UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: @ "ticket sold out" delegate: nil cancelButtonTitle: @ "" destructiveButtonTitle: @ "OK" comment: nil, nil]; [sheet showInView: self. view]; [sheet release]; break;} [self defined mselecw.mainthread: @ selector (updateView :) withObject: [[NSThread currentThread] name] waitUntilDone: YES]; [lock unlock]; // [_ ticketCondition unlock];}