1 //2 //VIEWCONTROLLER.M3 //06-Producer Consumer issues4 //5 //Created by Mac on 16/4/20.6 //copyright©2016 Year Mac. All rights reserved.7 //8 9 /*producer consumers dealing with thread synchronization issuesTen Ideas: One 1). The producer wants to obtain the lock, then produces (to the storehouse to put its production goods), if the storehouse is full, then wait, then releases the lock. Until the thread wakes it up again to produce it, and if it is not full, then send signal (message) to wake up the waiting consumer when the product is produced. A 2). The consumer wants to obtain the lock, then consumes (goes to the storehouse to fetch the product), if does not have the commodity, then wait, then releases the lock, until the thread calls it to consume, if has the commodity, after the consumption informs the wait producer - */ - #defineKmaxproductcount 10 the #import "ViewController.h" - - @interfaceViewcontroller () - + @end - + @implementationViewcontroller { A at //the combination of mutex and conditional lock -Nscondition *_condition; - - //Products (warehouse) -Nsmutablearray *_productarray; - } in -- (void) Viewdidload { to [Super Viewdidload]; + - [self addthread]; the } * $ /**Panax Notoginseng * Add (multiple) threads - */ the- (void) Addthread { + A_condition =[[Nscondition alloc] init]; the_productarray =[Nsmutablearray array]; + - [Nsthread detachnewthreadselector: @selector (producteraction) totarget:self Withobject:nil]; $ [Nsthread detachnewthreadselector: @selector (producteraction) totarget:self Withobject:nil]; $ [Nsthread detachnewthreadselector: @selector (producteraction) totarget:self Withobject:nil]; - [Nsthread detachnewthreadselector: @selector (consumeraction) totarget:self Withobject:nil]; - the } - /**Wuyi * Producer Producter the */ -- (void) producteraction { Wu - while(TRUE) { About //lock to prevent thread changes $[_conditionLock]; - - //judge if the storeroom is full and full, and the producers have to wait - while(Kmaxproductcount = =_productarray.count) { ANSLog (@"producers Wait"); + [_condition wait]; the } - $ //0.2-2s Simulation Producer production process: (Arc4random ()% + 1)/5.0 the[Nsthread Sleepfortimeinterval: (arc4random ()%Ten+1) /5.0]; the [_productarray Addobject:[[nsobject alloc] init]]; the theNSLog (@"Total =%li", _productarray.count); - in //Wake up a single Consumer object waiting on this condition the [_condition signal]; the About //broadcast, one-to-many: calling multiple consumers the [_condition broadcast]; the the [_condition unlock]; + } - } the Bayi /** the * Consumer Consumer the */ -- (void) consumeraction { - the while(TRUE) { the //lock to prevent thread changes the[_conditionLock]; the - //determine if the warehouse is empty, no, consumers have to wait the while(Kmaxproductcount = =0) { theNSLog (@"Empty and Hollow"); the [_condition wait];94 } the the //0.2-2s Simulation Producer production process: (Arc4random ()% + 1)/5.0 the[Nsthread Sleepfortimeinterval: (arc4random ()%Ten+1) /5.0];98 [_productarray Removelastobject]; About -NSLog (@"Total =%li", _productarray.count);101 102 //Wake up a single Consumer object waiting on this condition103 //[_condition signal];104 the //Wake up all consumer objects waiting on this condition106 [_condition unlock];107 }108 }109 the 111 @end
Wait and sleep differences:
1. From different class-->sleep (Nsthread class method) Wait (Nscondition class method)
2.sleep for Line programming (wait), wait for thread communication (send notification); (one wait, one notification)
The 3.sleep method does not release the lock and always consumes resources; Wait releases the lock, enters the thread pool, waits for the system resources. Other threads can use synchronous control blocks or methods, and other threads execute signal/broadcast to regain CPU execution time
Producer Consumer issues -05-Multithreading