IOS multithreading and thread synchronization

Source: Internet
Author: User

IOS multithreading and thread synchronization

Generally, we use threads to access the same resource in multiple threads. To ensure the security of thread resources and ensure the correctness of thread access.

In IOS, we generally use the following three methods to synchronize code:

The first and second types of code are used for synchronization. Generally, we only need to use NSLock and NSCondition to declare two attributes. Then assign the corresponding value to this attribute. As a thread for security prevention and control.

It can also ensure the thread resource security.

1: NSLock Mode

 

[Xxxlock lock] // lock

Synchronous Code Block

[Xxxlock unlock] // unlock

 

2: NSCondition Mode

[XxxCondition lock] // lock

Synchronous Code Block

[XxxCondition unlock] // unlock

 

Method 3: When synchronized is used, we generally only need to pass a self in brackets. When a thread enters the synchronization code block, the lock flag of the object in the brackets will be locked, and other threads will wait for the lock to open the other threads and then enter one. In this way, the security of thread-based question-free resources can be guaranteed.

3: @ synchronized (same object ){

Code execution by thread;

}

 

Sample Code for thread resource prevention and control:

-(Void) sellTickets {while (YES) {NSString * name = [NSThread currentThread]. name; // Synchronous Code block when a thread goes in, the lock flag of the object in the brackets will be locked, other threads will wait outside. When the threads going in will open the lock and the other threads will enter another // @ synchronized (self) {// [self. myLock lock]; [self. myCondition lock]; NSLog (@ "% @ start to sell the % d ticket", name, self. selledCount + 1); [NSThread sleepForTimeInterval :. 2]; self. selledCount ++; NSLog (@ "% @ sold the % d ticket, with % d tickets left", name, self. selledCount, self. totalCount-self.selledCount); // [self. myLock unlock]; [self. myCondition unlock];} //}

 

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.