ukim
ukim

Reputation: 2465

GCD: What happens when two threads want to execute blocks on the main thread at the same time

I am using GCD in my IOS app. I have threes threads: the main thread, thread 2, and thread 3.

The following code is executed on thread 2,

    dispatch_async(dispatch_get_main_queue(), ^{ code block 1 ...

so code bock 1 will be executed on the main thread. What happens if the following code is executed in thread 3 before code bock 1 finish running:

    dispatch_async(dispatch_get_main_queue(), ^{ code block 2 ...

will bock2 wait until bock1 terminates?

How can I find answers of such questions? Shall I read APPLE's documents or do some experiments myself? What kind of experiments can I do?

Upvotes: 0

Views: 295

Answers (2)

dezinezync
dezinezync

Reputation: 3012

The answer lies in the fact that the main thread is a serial queue. That is, in your example, block 2 will wait for block 1 to finish before it can be executed. Careful though, if your first block gets locked on something or is waiting for a long time, the execution of block 2 might be delayed for a long time or even indefinitely.

For a simple example you can run, you can refer to my answer on this question here: https://stackoverflow.com/a/20683252/1387258

What's happening there is:

  • The collectionView is requested to reload it's data on the main thread from a different thread.
  • The collectionView (for example) is then requested to add a new section, a bunch of items and such.

Now, this is vital if your second block is dependent on your first block. That is, you may need to first invalidate your layout, before you can add new items to it.

A second scenario could be: you need to change the layout on your collection view, before you can update it's contents.

How can I find answers of such.
I'm going to recommend you try various things like what I've suggested above. The main thread is for UI updates and such only. Try experimenting there and good luck.

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299275

The document you want is the Concurrency Programming Guide. In particular you want the section on Dispatch Queues, and somewhat more importantly you want the section on Migrating Away from Threads.

You should not think of yourself as having three threads (in fact, you may not). You may have three blocks. You may have three queues. How and if these are dispatched to threads is an internal implementation detail.

In GCD, the word "dispatch" means "place on a queue." When a block reaches the front of a system queue, it will be eligible to run on an available thread. Queues may feed into other queues, but eventually they have to tie to one of the system queues (otherwise they would never execute).

The main queue is a serial queue. Like other serial queues, each block must complete before the next block is allowed to run (this is why you can starve or deadlock the main queue if you're not careful). There are also concurrent queues, which only require that each block be started before the next is considered.

But the key is to remember that these are just FIFO queues that you can put blocks onto.

Upvotes: 2

Related Questions