شرح التزامن: كيفية إنشاء تطبيق iOS متعدد الخيوط

يعد التزامن في iOS موضوعًا ضخمًا. لذا في هذه المقالة أريد أن أركز على موضوع فرعي يتعلق بقوائم الانتظار وإطار عمل Grand Central Dispatch (GCD).

على وجه الخصوص ، أود استكشاف الاختلافات بين قوائم الانتظار التسلسلية والمتزامنة ، فضلاً عن الاختلافات بين التنفيذ المتزامن وغير المتزامن.

إذا لم تستخدم GCD من قبل ، فهذه المقالة هي مكان رائع للبدء. إذا كانت لديك بعض الخبرة مع GCD ، ولكنك لا تزال مهتمًا بالموضوعات المذكورة أعلاه ، أعتقد أنك ستظل تجدها مفيدة. وآمل أن تلتقط شيئًا أو شيئين جديدين على طول الطريق.

لقد أنشأت تطبيقًا مصاحبًا لـ SwiftUI لعرض المفاهيم الواردة في هذه المقالة بصريًا. يحتوي التطبيق أيضًا على اختبار قصير ممتع أشجعك على تجربته قبل قراءة هذا المقال وبعده. قم بتنزيل شفرة المصدر هنا ، أو احصل على الإصدار التجريبي العام هنا.

سأبدأ بمقدمة إلى GCD ، متبوعة بشرح تفصيلي للمزامنة ، غير المتزامن ، التسلسلي والمتزامن. بعد ذلك ، سأغطي بعض المخاطر عند العمل مع التزامن. أخيرًا ، سأختم بملخص وبعض النصائح العامة.

المقدمة

لنبدأ بمقدمة موجزة عن GCD وإرسال قوائم الانتظار. لا تتردد في التخطي إلى قسم Sync vs Async إذا كنت معتادًا على الموضوع.

التزامن وغراند سنترال ديسباتش

يتيح لك التزامن الاستفادة من حقيقة أن جهازك يحتوي على أنوية متعددة لوحدة المعالجة المركزية. للاستفادة من هذه النوى ، ستحتاج إلى استخدام خيوط متعددة. ومع ذلك ، فإن الخيوط أداة منخفضة المستوى ، وإدارة الخيوط يدويًا بطريقة فعالة أمر صعب للغاية.

تم إنشاء Grand Central Dispatch بواسطة Apple منذ أكثر من 10 سنوات كعملية تجريدية لمساعدة المطورين على كتابة تعليمات برمجية متعددة الخيوط دون إنشاء وإدارة الخيوط يدويًا بأنفسهم.

مع GCD ، اتبعت Apple نهج تصميم غير متزامنللمشكلة. بدلاً من إنشاء سلاسل الرسائل مباشرة ، يمكنك استخدام GCD لجدولة مهام العمل ، وسيقوم النظام بأداء هذه المهام نيابةً عنك من خلال الاستفادة المثلى من موارده. ستتعامل GCD مع إنشاء سلاسل الرسائل المطلوبة وستجدول مهامك على سلاسل الرسائل هذه ، وتحويل عبء إدارة سلاسل الرسائل من المطور إلى النظام.

ميزة كبيرة لـ GCD هي أنه لا داعي للقلق بشأن موارد الأجهزة أثناء كتابة التعليمات البرمجية المتزامنة. يدير GCD مجموعة خيوط من أجلك ، وسوف يتوسع من Apple Watch أحادية النواة وصولاً إلى MacBook Pro متعدد النواة.

قوائم انتظار الإرسال

هذه هي اللبنات الأساسية لـ GCD والتي تتيح لك تنفيذ كتل عشوائية من التعليمات البرمجية باستخدام مجموعة من المعلمات التي تحددها. دائمًا ما تبدأ المهام في قوائم انتظار الإرسال بأسلوب الوارد أولاً يصرف أولاً (FIFO). لاحظ أنني قلت لقد بدأت ، لأن وقت الانتهاء من مهامك يعتمد على عدة عوامل ، وليس مضمونًا أن يكون FIFO (المزيد عن ذلك لاحقًا.)

بشكل عام ، هناك ثلاثة أنواع من قوائم الانتظار المتاحة لك:

  • قائمة انتظار الإرسال الرئيسية (تسلسلية ، محددة مسبقًا)
  • قوائم الانتظار العامة (متزامنة ، محددة مسبقًا)
  • قوائم الانتظار الخاصة (يمكن أن تكون متسلسلة أو متزامنة ، تقوم بإنشائها)

يأتي كل تطبيق مع قائمة انتظار رئيسية ، وهي قائمة انتظار تسلسلية تنفذ المهام في السلسلة الرئيسية. قائمة الانتظار هذه مسؤولة عن رسم واجهة المستخدم للتطبيق والاستجابة لتفاعلات المستخدم (اللمس ، التمرير ، التحريك ، إلخ.) إذا حظرت قائمة الانتظار هذه لفترة طويلة ، فسيظهر تطبيق iOS الخاص بك متجمدًا ، وسيعرض تطبيق macOS الشاطئ السيئ السمعة الكرة / عجلة الغزل.

عند تنفيذ مهمة طويلة المدى (مكالمة شبكة ، عمل حسابي مكثف ، إلخ) ، نتجنب تجميد واجهة المستخدم من خلال أداء هذا العمل في قائمة انتظار في الخلفية. ثم نقوم بتحديث واجهة المستخدم بالنتائج الموجودة في قائمة الانتظار الرئيسية:

URLSession.shared.dataTask(with: url) { data, response, error in if let data = data { DispatchQueue.main.async { // UI work self.label.text = String(data: data, encoding: .utf8) } } }

كقاعدة عامة ، يجب تنفيذ جميع أعمال واجهة المستخدم في قائمة الانتظار الرئيسية. يمكنك تشغيل خيار Main Thread Checker في Xcode لتلقي تحذيرات عندما يتم تنفيذ عمل واجهة المستخدم على سلسلة في الخلفية.

يمكن العثور على مدقق الموضوع الرئيسي في محرر المخطط

بالإضافة إلى قائمة الانتظار الرئيسية ، يأتي كل تطبيق مع العديد من قوائم الانتظار المتزامنة المحددة مسبقًا والتي لها مستويات مختلفة من جودة الخدمة (فكرة مجردة للأولوية في GCD.)

على سبيل المثال ، إليك الكود لإرسال العمل بشكل غير متزامن إلى قائمة انتظار QoS التفاعلية للمستخدم (الأولوية القصوى):

DispatchQueue.global(qos: .userInteractive).async { print("We're on a global concurrent queue!") }

بدلاً من ذلك ، يمكنك استدعاء قائمة الانتظار العمومية الافتراضية ذات الأولوية من خلال عدم تحديد جودة الخدمة مثل هذا:

DispatchQueue.global().async { print("Generic global queue") }

بالإضافة إلى ذلك ، يمكنك إنشاء قوائم انتظار خاصة بك باستخدام الصيغة التالية:

let serial = DispatchQueue(label: "com.besher.serial-queue") serial.async { print("Private serial queue") }

عند إنشاء قوائم انتظار خاصة ، من المفيد استخدام تسمية وصفية (مثل تدوين DNS العكسي) ، حيث سيساعدك ذلك أثناء تصحيح الأخطاء في ملاح Xcode و lldb و Instruments:

بشكل افتراضي ، تكون قوائم الانتظار الخاصة مسلسلة (سأشرح ما يعنيه هذا قريبًا ، أعدك!) إذا كنت تريد إنشاء قائمة انتظار خاصة متزامنة ، فيمكنك القيام بذلك عبر معلمة السمات الاختيارية :

let concurrent = DispatchQueue(label: "com.besher.serial-queue", attributes: .concurrent) concurrent.sync { print("Private concurrent queue") }

هناك أيضًا معلمة QoS اختيارية. ستهبط قوائم الانتظار الخاصة التي تقوم بإنشائها في النهاية في إحدى قوائم الانتظار المتزامنة العالمية بناءً على معلماتها المحددة.

ماذا يوجد في المهمة؟

ذكرت إرسال المهام إلى قوائم الانتظار. يمكن أن تشير المهام إلى أي كتلة من التعليمات البرمجية ترسلها إلى قائمة انتظار باستخدام syncأو asyncوظائف. يمكن تقديمها في شكل إغلاق مجهول:

DispatchQueue.global().async { print("Anonymous closure") }

أو داخل عنصر عمل إرسال يتم تنفيذه لاحقًا:

let item = DispatchWorkItem(qos: .utility) { print("Work item to be executed later") }

بغض النظر عما إذا كنت ترسل بشكل متزامن أو غير متزامن ، وما إذا اخترت قائمة انتظار تسلسلية أو متزامنة ، سيتم تنفيذ جميع التعليمات البرمجية داخل مهمة واحدة سطرًا بسطر. يعد التزامن مناسبًا فقط عند تقييم مهام متعددة .

على سبيل المثال ، إذا كان لديك 3 حلقات داخل نفس المهمة ، فسيتم تنفيذ هذه الحلقات دائمًا بالترتيب:

DispatchQueue.global().async { for i in 0..<10 { print(i) } for _ in 0..<10 { print("?") } for _ in 0..<10 { print("?") } }

يقوم هذا الرمز دائمًا بطباعة عشرة أرقام من 0 إلى 9 ، متبوعة بعشر دوائر زرقاء ، متبوعة بعشرة قلوب مكسورة ، بغض النظر عن كيفية إرسال هذا الإغلاق.

يمكن أن يكون للمهام الفردية أيضًا مستوى جودة الخدمة الخاص بها (بشكل افتراضي تستخدم أولوية قائمة الانتظار الخاصة بها.) يؤدي هذا التمييز بين QoS QoS و QoS للمهمة إلى سلوك مثير للاهتمام سنناقشه في قسم انعكاس الأولوية.

By now you might be wondering what serial and concurrent are all about. You might also be wondering about the differences between sync and async when submitting your tasks. This brings us to the crux of this article, so let’s dive in!

Sync vs Async

When you dispatch a task to a queue, you can choose to do so synchronously or asynchronously using the sync and async dispatch functions. Sync and async primarily affect the source of the submitted task, that is the queue where it is being submitted from.

When your code reaches a sync statement, it will block the current queue until that task completes. Once the task returns/completes, control is returned to the caller, and the code that follows the sync task will continue.

Think of sync as synonymous with ‘blocking’.

An async statement, on the other hand, will execute asynchronously with respect to the current queue, and immediately returns control back to the caller without waiting for the contents of the async closure to execute. There is no guarantee as to when exactly the code inside that async closure will execute.

Current queue?

It may not be obvious what the source, or current, queue is, because it’s not always explicitly defined in the code.

For example, if you call your sync statement inside viewDidLoad, your current queue will be the Main dispatch queue. If you call the same function inside a URLSession completion handler, your current queue will be a background queue.

Going back to sync vs async, let’s take this example:

DispatchQueue.global().sync { print("Inside") } print("Outside") // Console output: // Inside // Outside

The above code will block the current queue, enter the closure and execute its code on the global queue by printing “Inside”, before proceeding to print “Outside”. This order is guaranteed.

Let’s see what happens if we try async instead:

DispatchQueue.global().async { print("Inside") } print("Outside") // Potential console output (based on QoS): // Outside // Inside

Our code now submits the closure to the global queue, then immediately proceeds to run the next line. It will likely print “Outside” before “Inside”, but this order isn’t guaranteed. It depends on the QoS of the source and destination queues, as well as other factors that the system controls.

Threads are an implementation detail in GCD — we do not have direct control over them and can only deal with them using queue abstractions. Nevertheless, I think it can be useful to ‘peek under the covers’ at thread behaviour to understand some challenges we might encounter with GCD.

For instance, when you submit a task using sync, GCD optimizes performance by executing that task on the current thread (the caller.)

There is one exception however, which is when you submit a sync task to the main queue —  doing so will always run the task on the main thread and not the caller. This behaviour can have some ramifications that we will explore in the priority inversion section.

Which one to use?

When submitting work to a queue, Apple recommends using asynchronous execution over synchronous execution. However, there are situations where sync might be the better choice, such as when dealing with race conditions, or when performing a very small task. I will cover these situations shortly.

One large consequence of performing work asynchronously inside a function is that the function can no longer directly return its values (if they depend on the async work that’s being done). It must instead use a closure/completion handler parameter to deliver the results.

To demonstrate this concept, let’s take a small function that accepts image data, performs some expensive computation to process the image, then returns the result:

func processImage(data: Data) -> UIImage? { guard let image = UIImage(data: data) else { return nil } // calling an expensive function let processedImage = upscaleAndFilter(image: image) return processedImage }

In this example, the function upscaleAndFilter(image:) might take several seconds, so we want to offload it into a separate queue to avoid freezing the UI. Let’s create a dedicated queue for image processing, and then dispatch the expensive function asynchronously:

let imageProcessingQueue = DispatchQueue(label: "com.besher.image-processing") func processImageAsync(data: Data) -> UIImage? { guard let image = UIImage(data: data) else { return nil } imageProcessingQueue.async { let processedImage = upscaleAndFilter(image: image) return processedImage } }

There are two issues with this code. First, the return statement is inside the async closure, so it is no longer returning a value to the processImageAsync(data:) function, and currently serves no purpose.

But the bigger issue is that our processImageAsync(data:) function is no longer returning any value, because the function reaches the end of its body before it enters the async closure.

To fix this error, we will adjust the function so that it no longer directly returns a value. Instead, it will have a new completion handler parameter that we can call once our asynchronous function has completed its work:

let imageProcessingQueue = DispatchQueue(label: "com.besher.image-processing") func processImageAsync(data: Data, completion: @escaping (UIImage?) -> Void) { guard let image = UIImage(data: data) else { completion(nil) return } imageProcessingQueue.async { let processedImage = self.upscaleAndFilter(image: image) completion(processedImage) } }

As evident in this example, the change to make the function asynchronous has propagated to its caller, who now has to pass in a closure and handle the results asynchronously as well. By introducing an asynchronous task, you can potentially end up modifying a chain of several functions.

Concurrency and asynchronous execution add complexity to your project as we just observed. This indirection also makes debugging more difficult. That’s why it really pays off to think about concurrency early in your design — it’s not something you want to tack on at the end of your design cycle.

Synchronous execution, by contrast, does not increase complexity. Rather, it allows you to continue using return statements as you did before. A function containing a sync task will not return until the code inside that task has completed. Therefore it does not require a completion handler.

If you are submitting a tiny task (for example, updating a value), consider doing it synchronously. Not only does that help you keep your code simple, it will also perform better — Async is believed to incur an overhead that outweighs the benefit of doing the work asynchronously for tiny tasks that take under 1ms to complete.

If you are submitting a large task, however, like the image processing we performed above, then consider doing it asynchronously to avoid blocking the caller for too long.

Dispatching on the same queue

While it is safe to dispatch a task asynchronously from a queue into itself (for example, you can use .asyncAfter on the current queue), you can not dispatch a task synchronously from a queue into the same queue. Doing so will result in a deadlock that immediately crashes the app!

This issue can manifest itself when performing a chain of synchronous calls that lead back to the original queue. That is, you sync a task onto another queue, and when the task completes, it syncs the results back into the original queue, leading to a deadlock. Use async to avoid such crashes.

Blocking the main queue

Dispatching tasks synchronously from the main queue will block that queue, thereby freezing the UI, until the task is completed. Thus it’s better to avoid dispatching work synchronously from the main queue unless you’re performing really light work.

Serial vs Concurrent

Serial and concurrent affect the destination —  the queue in which your work has been submitted to run. This is in contrast to sync and async, which affected the source.

A serial queue will not execute its work on more than one thread at a time, regardless of how many tasks you dispatch on that queue. Consequently, the tasks are guaranteed to not only start, but also terminate, in first-in, first-out order.

Moreover, when you block a serial queue (using a sync call, semaphore, or some other tool), all work on that queue will halt until the block is over.

A concurrent queue can spawn multiple threads, and the system decides how many threads are created. Tasks always start in FIFO order, but the queue does not wait for tasks to finish before starting the next task, therefore tasks on concurrent queues can finish in any order.

When you perform a blocking command on a concurrent queue, it will not block the other threads on this queue. Additionally, when a concurrent queue gets blocked, it runs the risk of thread explosion. I will cover this in more detail later on.

The main queue in your app is serial. All the global pre-defined queues are concurrent. Any private dispatch queue you create is serial by default, but can be set to be concurrent using an optional attribute as discussed earlier.

It’s important to note here that the concept of serial vs concurrent is only relevant when discussing a specific queue. All queues are concurrent relative to each other.

That is, if you dispatch work asynchronously from the main queue to a private serial queue, that work will be completed concurrently with respect to the main queue. And if you create two different serial queues, and then perform blocking work on one of them, the other queue is unaffected.

To demonstrate the concurrency of multiple serial queues, let’s take this example:

let serial1 = DispatchQueue(label: "com.besher.serial1") let serial2 = DispatchQueue(label: "com.besher.serial2") serial1.async { for _ in 0..<5 { print("?") } } serial2.async { for _ in 0..<5 { print("?") } }

Both queues here are serial, but the results are jumbled up because they execute concurrently in relation to each other. The fact that they’re each serial (or concurrent) has no effect on this result. Their QoS level determines who will generally finish first (order not guaranteed).

If we want to ensure that the first loop finishes first before starting the second loop, we can submit the first task synchronously from the caller:

let serial1 = DispatchQueue(label: "com.besher.serial1") let serial2 = DispatchQueue(label: "com.besher.serial2") serial1.sync { // <---- we changed this to 'sync' for _ in 0..<5 { print("?") } } // we don't get here until first loop terminates serial2.async { for _ in 0..<5 { print("?") } }

This is not necessarily desirable, because we are now blocking the caller while the first loop is executing.

To avoid blocking the caller, we can submit both tasks asynchronously, but to the same serial queue:

let serial = DispatchQueue(label: "com.besher.serial") serial.async { for _ in 0..<5 { print("?") } } serial.async { for _ in 0..<5 { print("?") } } 

Now our tasks execute concurrently with respect to the caller, while also keeping their order intact.

Note that if we make our single queue concurrent via the optional parameter, we go back to the jumbled results, as expected:

let concurrent = DispatchQueue(label: "com.besher.concurrent", attributes: .concurrent) concurrent.async { for _ in 0..<5 { print("?") } } concurrent.async { for _ in 0..<5 { print("?") } }

Sometimes you might confuse synchronous execution with serial execution (at least I did), but they are very different things. For example, try changing the first dispatch on line 3 from our previous example to a sync call:

let concurrent = DispatchQueue(label: "com.besher.concurrent", attributes: .concurrent) concurrent.sync { for _ in 0..<5 { print("?") } } concurrent.async { for _ in 0..<5 { print("?") } }

Suddenly, our results are back in perfect order. But this is a concurrent queue, so how could that happen? Did the sync statement somehow turn it into a serial queue?

The answer is no!

This is a bit sneaky. What happened is that we did not reach the async call until the first task had completed its execution. The queue is still very much concurrent, but inside this zoomed-in section of the code. it appears as if it were serial. This is because we are blocking the caller, and not proceeding to the next task, until the first one is finished.

If another queue somewhere else in your app tried submitting work to this same queue while it was still executing the sync statement, that work will run concurrently with whatever we got running here, because it’s still a concurrent queue.

Which one to use?

Serial queues take advantage of CPU optimizations and caching, and help reduce context switching.

Apple recommends starting with one serial queue per subsystem in your app —  for example one for networking, one for file compression, etc. If the need arises, you can later expand to a hierarchy of queues per subsystem using the setTarget method or the optional target parameter when building queues.

If you run into a performance bottleneck, measure your app’s performance then see if a concurrent queue helps. If you do not see a measurable benefit, it’s better to stick to serial queues.

Pitfalls

Priority Inversion and Quality of Service

Priority inversion is when a high priority task is prevented from running by a lower priority task, effectively inverting their relative priorities.

This situation often occurs when a high QoS queue shares a resources with a low QoS queue, and the low QoS queue gets a lock on that resource.

But I wish to cover a different scenario that is more relevant to our discussion —  it’s when you submit tasks to a low QoS serial queue, then submit a high QoS task to that same queue. This scenario also results in priority inversion, because the high QoS task has to wait on the lower QoS tasks to finish.

GCD resolves priority inversion by temporarily raising the QoS of the queue that contains the low priority tasks that are ‘ahead’ of, or blocking, your high priority task.

It’s kind of like having cars stuck in frontof an ambulance. Suddenly they’re allowed to cross the red light just so that the ambulance can move (in reality the cars move to the side, but imagine a narrow (serial) street or something, you get the point :-P)

To illustrate the inversion problem, let’s start with this code:

 enum Color: String { case blue = "?" case white = "⚪️" } func output(color: Color, times: Int) { for _ in 1...times { print(color.rawValue) } } let starterQueue = DispatchQueue(label: "com.besher.starter", qos: .userInteractive) let utilityQueue = DispatchQueue(label: "com.besher.utility", qos: .utility) let backgroundQueue = DispatchQueue(label: "com.besher.background", qos: .background) let count = 10 starterQueue.async { backgroundQueue.async { output(color: .white, times: count) } backgroundQueue.async { output(color: .white, times: count) } utilityQueue.async { output(color: .blue, times: count) } utilityQueue.async { output(color: .blue, times: count) } // next statement goes here }

We create a starter queue (where we submit the tasks from), as well as two queues with different QoS. Then we dispatch tasks to each of these two queues, each task printing out an equal number of circles of a specific colour (utility queueis blue, background is white.)

Because these tasks are submitted asynchronously, every time you run the app, you’re going to see slightly different results. However, as you would expect, the queue with the lower QoS (background) almost always finishes last. In fact, the last 10–15 circles are usually all white.

But watch what happens when we submit a sync task to the background queue after the last async statement. You don’t even need to print anything inside the sync statement, just adding this line is enough:

// add this after the last async statement, // still inside starterQueue.async backgroundQueue.sync {}

The results in the console have flipped! Now, the higher priority queue (utility) always finishes last, and the last 10–15 circles are blue.

To understand why that happens, we need to revisit the fact that synchronous work is executed on the caller thread (unless you’re submitting to the main queue.)

In our example above, the caller (starterQueue) has the top QoS (userInteractive.) Therefore, that seemingly innocuous sync task is not only blocking the starter queue, but it’s also running on the starter’s high QoS thread. The task therefore runs with high QoS, but there are two other tasks ahead of it on the same background queue that have background QoS. Priority inversion detected!

As expected, GCD resolves this inversion by raising the QoS of the entire queue to temporarily match the high QoS task. Consequently, all the tasks on the background queue end up running at user interactive QoS, which is higher than the utility QoS. And that’s why the utility tasks finish last!

Side-note: If you remove the starter queue from that example and submit from the main queue instead, you will get similar results, as the main queue also has user interactive QoS.

To avoid priority inversion in this example, we need to avoid blocking the starter queue with the sync statement. Using async would solve that problem.

Although it’s not always ideal, you can minimize priority inversions by sticking to the default QoS when creating private queues or dispatching to the global concurrent queue.

Thread explosion

When you use a concurrent queue, you run the risk of thread explosion if you’re not careful. This can happen when you try to submit tasks to a concurrent queue that is currently blocked (for example with a semaphore, sync, or some other way.) Your tasks will run, but the system will likely end up spinning up new threads to accommodate these new tasks, and threads aren’t cheap.

This is likely why Apple suggests starting with a serial queue per subsystem in your app, as each serial queue can only use one thread. Remember that serial queues are concurrent in relationto other queues, so you still get a performance benefit when you offload your work to a queue, even if it isn’t concurrent.

Race conditions

Swift Arrays, Dictionaries, Structs, and other value types are not thread-safe by default. For example, when you have multiple threads trying to access and modify the same array, you will start running into trouble.

There are different solutions to the readers-writers problem, such as using locks or semaphores. But the relevant solution I wish to discuss here is the use of an isolation queue.

Let’s say we have an array of integers, and we want to submit asynchronous work that references this array. As long as our work only reads the array and does not modify it, we are safe. But as soon as we try to modify the array in one of our asynchronous tasks, we will introduce instability in our app.

It’s a tricky problem because your app can run 10 times without issues, and then it crashes on the 11th time. One very handy tool for this situation is the Thread Sanitizer in Xcode. Enabling this option will help you identify potential race conditions in your app.

يمكن الوصول إلى معقم الخيط في محرر المخطط

To demonstrate the problem, let’s take this (admittedly contrived) example:

class ViewController: UIViewController { let concurrent = DispatchQueue(label: "com.besher.concurrent", attributes: .concurrent) var array = [1,2,3,4,5] override func viewDidLoad() { for _ in 0...1 { race() } } func race() { concurrent.async { for i in self.array { // read access print(i) } } concurrent.async { for i in 0..<10 { self.array.append(i) // write access } } } }

One of the async tasks is modifying the array by appending values. If you try running this on your simulator, you might not crash. But run it enough times (or increase the loop frequency on line 7), and you will eventually crash. If you enable the thread sanitizer, you will get a warning every time you run the app.

To deal with this race condition, we are going to add an isolation queue that uses the barrier flag. This flag allows any outstanding tasks on the queue to finish, but blocks any further tasks from executing until the barrier task is completed.

Think of the barrier like a janitor cleaning a public restroom (shared resource.) There are multiple (concurrent) stalls inside the restroom that people can use.

Upon arrival, the janitor places a cleaning sign (barrier) blocking any newcomers from entering until the cleaning is done, but the janitor does not start cleaning until all the people inside have finished their business. Once they all leave, the janitor proceeds to clean the public restroom in isolation.

When finally done, the janitor removes the sign (barrier) so that the people who are queued up outside can finally enter.

Here’s what that looks like in code:

class ViewController: UIViewController { let concurrent = DispatchQueue(label: "com.besher.concurrent", attributes: .concurrent) let isolation = DispatchQueue(label: "com.besher.isolation", attributes: .concurrent) private var _array = [1,2,3,4,5] var threadSafeArray: [Int] { get { return isolation.sync { _array } } set { isolation.async(flags: .barrier) { self._array = newValue } } } override func viewDidLoad() { for _ in 0...15 { race() } } func race() { concurrent.async { for i in self.threadSafeArray { print(i) } } concurrent.async { for i in 0..<10 { self.threadSafeArray.append(i) } } } }

We have added a new isolation queue, and restricted access to the private array using a getter and setter that will place a barrier when modifying the array.

The getter needs to be sync in order to directly return a value. The setter can be async, as we don’t need to block the caller while the write is taking place.

We could have used a serial queue without a barrier to solve the race condition, but then we would lose the advantage of having concurrent read access to the array. Perhaps that makes sense in your case, you get to decide.

Conclusion

Thank you so much for reading this far! I hope you learned something new from this article. I will leave you with a summary and some general advice:

Summary

  • Queues always start their tasks in FIFO order
  • Queues are always concurrent relative to other queues
  • Sync vs Async concerns the source
  • Serial vs Concurrent concerns the destination
  • Sync is synonymous with ‘blocking’
  • Async immediately returns control to caller
  • Serial uses a single thread, and guarantees order of execution
  • Concurrent uses multiple-threads, and risks thread explosion
  • Think about concurrency early in your design cycle
  • Synchronous code is easier to reason about and debug
  • Avoid relying on global concurrent queues if possible
  • Consider starting with a serial queue per subsystem
  • Switch to concurrent queue only if you see a measurable performance benefit

أنا أحب الاستعارة من Swift Concurrency Manifesto بوجود "جزيرة من التسلسل في بحر من التزامن". تمت مشاركة هذا الشعور أيضًا في هذه التغريدة بواسطة Matt Diephouse:

سر كتابة الكود المتزامن هو جعل معظمه مسلسلاً. قصر التزامن على طبقة خارجية صغيرة. (قلب تسلسلي ، غلاف متزامن.)

على سبيل المثال ، بدلاً من استخدام قفل لإدارة 5 خصائص ، أنشئ نوعًا جديدًا يلتف عليها واستخدم خاصية واحدة داخل القفل.

- مات ديبهاوس (mdiep) 18 ديسمبر 2019

عندما تقوم بتطبيق التزامن مع هذه الفلسفة في الاعتبار ، أعتقد أنه سيساعدك على تحقيق رمز متزامن يمكن تفسيره دون أن تضيع في فوضى عمليات الاسترجاعات.

إذا كانت لديك أي أسئلة أو تعليقات ، فلا تتردد في التواصل معي على Twitter

بشر المالح

صورة الغلاف بواسطة Onur K على Unsplash

قم بتنزيل التطبيق المصاحب من هنا:

تطبيق almaleh / Dispatcher Companion لمقالتي حول التزامن. المساهمة في تطوير almaleh / Dispatcher عن طريق إنشاء حساب على جيثب. جيثب المالح

تحقق من بعض مقالاتي الأخرى:

Fireworks - محرر جسيمات مرئية لـ Swift قم بإنشاء رمز Swift سريعًا لنظامي التشغيل macOS و iOS أثناء قيامك بالتصميم والتكرار على تأثيرات الجسيمات بشر المالح لا تشوبه شائبة iOS أنت لست بحاجة (دائمًا) إلى [ضعف النفس] في هذه المقالة ، سوف نتحدث عن ضعف الذات داخل إغلاق Swift لتجنب دورات الاحتفاظ واستكشاف الحالات التي قد يكون أو لا يكون فيها ضروريًا لالتقاط الذات بشكل ضعيف. بشر المالح فلولس iOS

قراءة متعمقة:

مقدمة توضح كيفية تنفيذ مسارات التعليمات البرمجية المتزامنة في أحد التطبيقات. البرمجة المتزامنة: واجهات برمجة التطبيقات والتحديات · objc.io objc.io تنشر كتبًا حول التقنيات والممارسات المتقدمة لتطوير iOS و OS X تنشر Florian Kugler Low-Level Concurrency APIs · objc.io objc.io تنشر كتبًا عن التقنيات والممارسات المتقدمة لنظام iOS تطوير OS X Daniel Eggert

//khanlou.com/2016/04/the-GCD-handbook/

أنا أكافح لفهم قوائم الانتظار المتزامنة والمتسلسلة في GCD بشكل كامل. لدي بعض المشكلات وآمل أن يتمكن أحدهم من الإجابة بشكل واضح وفي هذه اللحظة ، أنا أقرأ أن قوائم الانتظار التسلسلية قد تم إنشاؤها ... Bogdan Alexandru Stack Overflow

مقاطع فيديو WWDC:

تحديث استخدام إرسال Grand Central - WWDC 2017 - مقاطع فيديو - أعاد مطور Apple macOS 10.13 و iOS 11 ابتكار كيفية تعاون Grand Central Dispatch و Darwin kernel ، مما يتيح تشغيل تطبيقاتك ... مطور Apple يبني تطبيقات مستجيبة وفعالة باستخدام GCD - WWDC 2015 - مقاطع الفيديو - تضع تطبيقات Apple Developer watchOS و iOS Multitasking طلبات متزايدة على كفاءة تطبيقك واستجابته. بتوجيه من الخبراء من ... Apple Developer