Thread Synchronization-Inter-thread Communication

Source: Internet
Author: User

I. Thread Synchronization

The thread synchronization method is similar to that in other systems. We can use atomic operations, mutex, lock, and so on.

IOS atomic operation functions start with OSAtomic, such as OSAtomicAdd32 and OSAtomicOr32. These functions can be used directly because they are atomic operations.

In iOS, mutex corresponds to NSLock, which follows the NSLooking protocol. We can use lock, tryLock, and lockBeforeData: To lock and unLock with unLock. Example:

BOOL moreToDo = YES;

NSLock * theLock = [[NSLock alloc] init];

...

While (moreToDo ){

/* Do another increment of calculation * // * until there's no more to do .*/

If ([theLock tryLock]) {

/* Update display used by all threads .*/

[TheLock unlock];}

}

We can use the command @ synchronized to simplify NSLock usage, so that we don't have to show the code for creating NSLock, locking and unlocking. -(Void) myMethod :( id) anObj

{

@ Synchronized (anObj ){

// Everything between the braces is protected by the @ synchronized directive .}

}

There are other lock objects, such as the loop lock NSRecursiveLock, the conditional lock NSConditionLock, and the distributed lock NSDistributedLock. Here we will not discuss them one by one. Please refer to the official documents.

Sequence of synchronous execution with NSCodition

NSCodition is a special type of lock that we can use to synchronize the sequence of Operation execution. The difference between it and mutex is that it is more accurate. Wait until a NSCondtion thread is locked until other threads send a signal to that condition. The following is an example:

A thread is waiting for things to be done, and there is no things to be done by other threads to notify it.

[CocoaCondition lock]; while (timeToDoWork <= 0)

[CocoaCondition wait];

TimeToDoWork --;

// Do real work here. [cocoaCondition unlock];

Other threads can send signals to the above threads to do things:

[CocoaCondition lock]; timeToDoWork ++; [cocoaCondition signal]; [cocoaCondition unlock];

2. Inter-thread Communication

A thread may need to communicate with other threads during running. We can use some NSObject methods to do things in the main thread of the application:

When mselec1_mainthread: withObject: waitUntilDone: When mselec1_mainthread: withObject: waitUntilDone: modes:

Do things in the specified Thread:

PerformSelector: onThread: withObject: waitUntilDone: modes:

Do things in the current thread:

PerformSelector: withObject: afterDelay: inModes:

Cancels a message sent to the current thread.

CancelPreviousPerformRequestsWithTarget: selector: object:

For example, to download data in a thread and notify the main thread to update the interface after the download is complete, you can use the following interface:-(void) myThreadMainMethod

{

NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];

// To do something in your thread job

...

[Self defined mselecw.mainthread: @ selector (updateUI) withObject: nil waitUntilDone: NO]; [pool release];

}

RunLoop

When it comes to NSThread, we can't help but talk about the closely related nsunloop. Run loop is equivalent to the Message loop Mechanism in win32. It allows you to schedule busy or Idle threads Based on the event/message (mouse message, Keyboard Message, Timer message, etc. The system automatically generates a corresponding run loop for the main thread of the application to process its message loop. When a UIView is touched, the function such as touchesBegan/touchesMoved can be called because the main thread of the application has such a run loop in the UIApplicationMain to distribute input or timer events.

 

Related Article

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.