Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a GCD queue which is always serial, even on multi-core CPUs?

As far as I understand non-main-queue GCD queues, they are serial by default only on devices with single-core CPUs. But if a device has multiple cores, it can happen that blocks in the queue get executed simultaneously.

I want to use a serial GCD queue to overcome some concurrency problems and this queue must be serial even if there are multiple cores.

A developer mentioned this is possible somehow. How would I create such a always-serial queue?

like image 743
International Frog Avatar asked Aug 15 '12 12:08

International Frog


2 Answers

Standard GCD queues that can be obtained with dispatch_get_global_queue function are concurrent indeed.

But you can create custom gcd queue using dispatch_queue_create function. Pass DISPATCH_QUEUE_SERIAL as a second parameter to create that queue as serial.

like image 69
Vladimir Avatar answered Sep 28 '22 17:09

Vladimir


To create concurrent queue:
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.aj.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);

To create serial queue:
dispatch_queue_t serialQueue = dispatch_queue_create("com.aj.serial.queue", DISPATCH_QUEUE_SERIAL);

like image 40
Tai Le Avatar answered Sep 28 '22 18:09

Tai Le