Using Nscondition for multithreaded synchronization

Source: Internet
Author: User

There are many ways to implement multithreaded technology in iOS.

Here is the problem of using nscondition to realize multithreading synchronization, which is to solve the problem of producer consumers (such as sending and receiving synchronization, etc.).


The problem flow is as follows:The consumer obtains the lock, takes the product, if does not have, then wait, then will release the lock, until the thread wakes it to consume the product;

The producer manufactures the product, first also must obtain the lock, then produces, then sends the signal, this can awaken wait the consumer.


Here are some things to watch for wait and signal:

1: In fact, the wait function inside quietly call the unlock function (guess, interested in self-analysis), that is, after calling the Wati function, the Nscondition object is in a lock-free State, This allows other threads to lock the object and trigger the Nscondition object. When Nscondition is triggered by another thread, the notification is triggered inside the wait function, and then the lock function (guess) is recalled for this event. and externally it looks as if the thread that receives the event (the thread that calls wait) never releases ownership of the Nscondition object, and the Wati thread enters the trigger state directly from the blocking state. It is easy to cause misunderstanding here.

The 2:wait function is not completely trustworthy. That is, when wait returns, does not mean that the corresponding event must be triggered, so in order to ensure synchronization between threads, the use of nscondtion often need to add an additional variable to avoid the abnormal wait return.

3: The order of calls for multiple wait, the test discovery is related to the wait execution order. Please consult the documentation for details.


the test code is as follows (please create your own project and connect two buttons to Test1button and Test2button):


viewcontroller.m//condtiontest////Created by Cuibo on 11/12/14.//Copyright (c) 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];            Additional setup after loading the view, typically from a nib.    Self.products = [[Nsmutablearray alloc] init]; Self.condition = [[Nscondition alloc] init];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} Click to start accepting-(ibaction) Test1button: (ID) sender{[nsthread detachnewthreadselector: @selector (thread1) totarget:self Withobject:nil];} Click to indicate receipt of a data-(ibaction) Test2button: (ID) sender{[nsthread detachnewthreadselector: @selector (thread2) Totarget: Self Withobject:nil];} Send thread-(void) thread1{while (1) {NSLog (@ "ThreaD1: Waiting to be sent ");        [Self.condition Lock];                [Self.condition wait];        NSLog (@ "Thread1: send");    [Self.condition unlock];    }}//receive thread-(void) thread2{[self.condition Lock];    NSLog (@ "THREAD2: Received data");    [Self.condition signal]; [Self.condition unlock];} @end


Using Nscondition for multithreaded synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.