A: Resource lock
#import <GHUnitIOS/GHUnit.h>
@interface WPGlobalTest : GHTestCase{ int _count; NSLock *_lock;}@end
-(void)test3{ if (_lock == nil) { _lock = [[NSLock alloc] init]; } NSThread *t1 = [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease]; [t1 setName:@"t1"]; [t1 start]; NSThread *t2 = [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease]; [t2 setName:@"t2"]; [t2 start]; NSThread *t3 = [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease]; [t3 setName:@"t3"]; [t3 start]; }-(void)run{ NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc] init]; while (1) { //[_lock lock]; _count++; NSThread *currentThread = [NSThread currentThread]; NSLog(@"current thread name -> %@, count -> %d", [currentThread name], _count); [NSThread sleepForTimeInterval:1]; //[_lock unlock]; } [aPool drain];}@end
There is no lock (Comment [_ lock] and [_ Lock unlock]), the resource (_ count) will conflict.
13:20:14. 340 tests [7526: 14903] current thread name-> T3, Count-> 10
13:20:14. 340 tests [7526: 14403] current thread name-> T2, Count-> 11
13:20:14. 340 tests [7526: 14103] current thread name-> T1, Count-> 10
13:20:15. 343 tests [7526: 14403] current thread name-> T2, Count-> 13
13:20:15. 343 tests [7526: 14903] current thread name-> T3, Count-> 12
13:20:15. 343 tests [7526: 14103] current thread name-> T1, Count-> 12
13:20:16. 347 tests [7526: 14903] current thread name-> T3, Count-> 15
13:20:16. 347 tests [7526: 14103] current thread name-> T1, Count-> 14
13:20:16. 347 tests [7526: 14403] current thread name-> T2, Count-> 14
Resources do not conflict after the lock is applied.
13:23:53. 325 tests [7639: 14103] current thread name-> T1, Count-> 1
13:23:53. 326 tests [7639: 10703] wpglobaltest/test3 0.00 s
13:23:54. 327 tests [7639: 14403] current thread name-> T2, Count-> 2
13:23:55. 329 tests [7639: 14b03] current thread name-> T3, Count-> 3
13:23:56. 332 tests [7639: 14103] current thread name-> T1, Count-> 4
13:23:57. 334 tests [7639: 14403] current thread name-> T2, Count-> 5
13:23:58. 337 tests [7639: 14b03] current thread name-> T3, Count-> 6
13:23:59. 339 tests [7639: 14103] current thread name-> T1, Count-> 7
13:24:00. 340 tests [7639: 14403] current thread name-> T2, Count-> 8
13:24:01. 343 tests [7639: 14b03] current thread name-> T3, Count-> 9
B: Conditional lock example
# Import <ghunitios/ghunit. h> @ interface wpglobaltest: ghtestcase {int _ count; nslock * _ Lock; // resource lock nscondition * _ condition; // condition lock bool _ isok;} @ end
-(Void) test4 {If (_ condition = nil) {_ condition = [[nscondition alloc] init]; _ isok = no;} [nsthread detachnewthreadselector: @ selector (run1) totarget: Self withobject: Nil]; [nsthread sleepfortimeinterval: 10]; [nsthread detachnewthreadselector: @ selector (run2) totarget: Self withobject: Nil];} -(void) run1 {nslog (@ "Start run1... "); [_ condition lock]; // lock while (! _ Isok) {nslog (@ "run1 waiting... "); [_ condition Wait];} [_ condition unlock]; nslog (@" Stop run1... ");}-(void) run2 {nslog (@" Start run2... "); [_ condition lock]; _ isok = yes; nslog (@" change value... "); [_ condition signal]; nslog (@" signal run2... "); [_ condition unlock]; nslog (@" Stop run2... ");} @ end
Result:
14:17:32. 548 tests [9044: 14103] Start run1...
14:17:32. 549 tests [9044: 14103] run1 waiting...
14:17:42. 549 tests [9044: 12407] Start run2...
14:17:42. 550 tests [9044: 12407] change value...
14:17:42. 550 tests [9044: 10703] wpglobaltest/test4 10.00 s
14:17:42. 552 tests [9044: 12407] signal run2...
14:17:42. 553 tests [9044: 14103] Stop run1...
14:17:42. 553 tests [9044: 12407] Stop run2...