Multi-thread synchronization using NSCondition and multi-thread nscondition
There are many ways to implement multithreading in iOS.
Here we will talk about the problem of using NSCondition to implement multi-thread synchronization, that is, to solve the producer and consumer problems (such as sending and receiving synchronization and so on ).
The problem process is as follows:The consumer acquires the lock and obtains the product. If not, wait releases the lock until a thread wakes it up to consume the product;
The producer must first obtain the lock for the product to be manufactured and then generate the product and then send the signal to wake up the consumers of wait.
Pay attention to the wait and signal problems:
1: Actually, the wait function quietly calls the unlock function (guess, you can analyze it yourself). That is to say, after the wati function is called, The NSCondition object is in the unlocked state, in this way, other threads can lock this object and trigger this NSCondition object. When NSCondition is triggered by another thread, the wait function receives a notification that this event is triggered, and then calls the lock function again (speculation ), externally, it seems that the thread that receives the event (the thread that calls wait) never releases the ownership of the NSCondition object. The wati thread enters the trigger state directly from the blocking state. This may cause misunderstanding.
2: The wait function is not completely trusted. That is to say, after wait returns, it does not mean that the corresponding event is triggered. Therefore, to ensure the synchronization relationship between threads, when NSCondtion is used, an additional variable is often required to avoid abnormal wait return.
3: The call sequence of multiple wait instances is determined by the wait execution sequence. For more information, see the documentation.
Go to github to download the test project:
Https://github.com/zcsoft/ZCTest_NSCondtion
Main code snippets:
//// ViewController. m // CondtionTest /// Created by cuibo on 11/12/14. // Copyright (c) 2014 cuibo. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (strong, nonatomic) NSCondition * condition; @ property (strong, nonatomic) NSMutableArray * products; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self. products = [[NSMutableArray alloc] init]; self. condition = [[NSCondition alloc] init];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} // click it to start accepting-(IBAction) test1Button :( id) sender {[NSThread detachNewThreadSelector: @ selector (thread1) toTarget: self withObject: nil];} // click it to receive a data-(IBAction) test2Button :( id) sender {[NSThread detachNewThreadSelector: @ selector (thread2) toTarget: self withObject: nil];} // sending thread-(void) thread1 {while (1) {NSLog (@ "thread1: Waiting for sending"); [self. condition lock]; [self. condition wait]; NSLog (@ "thread1: Send"); [self. condition unlock] ;}}// receiving thread-(void) thread2 {[self. condition lock]; NSLog (@ "thread2: receive data"); [self. condition signal]; [self. condition unlock];} @ end