iOS Development - Multithreaded Synchronization

iOS Development - Multithreaded Synchronization

[[141566]]

Hi, today I want to talk to you about multi-thread synchronization in iOS development. When it comes to multi-thread synchronization, you may immediately think of various thread locks such as NSLock, NSCondtion, and even OSSpinLock. However, today we will not talk about thread locks. If you are interested in Lock, you can read this article. Since we are not talking about thread locks, what are we talking about? Of course, we are talking about multi-thread synchronization.

Thread synchronization is not the same as thread lock. To solve the synchronization problem, we first need to understand why thread synchronization is needed. The main reason for thread asynchrony is that multiple threads may operate an object at the same time, resulting in inconsistent states. Can we understand it this way? If multiple threads do not access an object at the same time, the synchronization problem is solved. How to do this? You can use the idea of ​​a serial queue. What is a serial queue? It can be simply understood that all operations must be executed in sequence. The main thread is a serial queue. The simplest way to synchronize is to put the synchronization operation on the main thread for execution. It doesn't matter. I didn't say it.

Since it cannot be executed in the main thread, wouldn't it be OK to put it in a child thread? There are many ways to create a child thread, so I won't repeat them here. Those who are interested can show their skills.

GCD synchronization

GCD may be one of the simplest ways to create child threads. This article has a detailed introduction to GCD, so I will not describe it further. Since we want to achieve thread synchronization, we first need to create a serial queue.

  1. _queue = dispatch_queue_create( "com.olinone.synchronize.serialQueue" , ​​NULL);
  2. dispatch_queue_t dQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 );
  3. dispatch_set_target_queue(_queue, dQueue);

Then, all synchronization events are added to the queue in sequence to ensure multi-thread synchronization

  1. dispatch_async(_queue, ^{
  2. block();
  3. });

NSOperationQueue synchronization

Most of the requirements can be met by GCD, but there are some special requirements, such as canceling the operation. In this case, NSOperationQueue will be a good choice.

Although NSOperationQueue is a concurrent multi-thread pool, its clever design can also make it realize the function of a serial queue. When maxConcurrentOperationCount=1, only one NSOperation is executed at the same time, and NSOperationQueue changes from concurrent execution to serial execution.

  1. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
  2. operationQueue.maxConcurrentOperationCount = 1 ;

The implementation method is the same as GCD. The serial execution of synchronous operations can be achieved by adding synchronous operations to the thread pool one by one.

  1. - ( void )execSyncBlock:( void (^)())block {
  2. if (NSOperationQueue.currentQueue == self) {
  3. block();
  4. } else {
  5. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:block];
  6. [self addOperations:@[operation] waitUntilFinished:YES];
  7. }
  8. }

HJSynchronizeDemo shows the actual usage in detail. Clever use of serial queues can bring unexpected results. In addition to solving multi-threaded synchronization, it can also handle serial large-scale computing services, such as image rendering, audio processing, etc.

PS: Our company is currently looking for high-end iOS development engineers. Interested friends can contact me for internal recommendation! The salary and benefits are great, and you can get a lot of Alibaba stocks. You will also have teachers Xiaosong and Song Ke to guide you, and you can also see Huachen's true appearance. Why not act quickly! You can forget about liking, but don't forget me for internal recommendation!

<<:  iOS development multithreaded synchronization

>>:  Linus Torvalds sends an open letter: blasting the Google Mail team

Recommend

Trash! Why is the usage rate so low every time a coupon is issued?

Coupons are widely used, from offline stores to o...

Father’s Day copy is here, it’s from the heart, not the body! !

Father's Day is coming. They are good at many...

From now on, the best Android phones come from Google

Google is getting serious about hardware. Some ti...

Do walls really block mobile phone and WiFi signals?

If you are always holding your mobile phone, you ...

Analysis of major mainstream information flow promotion channels in 2020

This article summarizes the characteristics of ma...

[3D Tour] Overlooking the national park from space, amazing animals are here!

Your browser does not support the video tag Reali...

Analyze these 4 factors of the viral growth model

What is Growth Hacking ? This concept originated ...

Mobile networks are killing Wi-Fi, survey shows

According to foreign media reports, recently, Man...