With GCD, you can create your own serial dispatch queues (see Different Types of Dispatch Queues for serial queues). Serial dispatch queues run their tasks in a first-in-first-out (FIFO) fashion. The asynchronous tasks on serial queues will not be performed on the main thread, however, making serial queues highly desirable for concurrent FIFO tasks.
All synchronous tasks submitted to a serial queue will be executed on the current thread being used by the code that is submitting the task, whenever possible. But asynchronous tasks submitted to a serial queue will always be executed on a thread other than the main thread.
We’ll use the dispatch_queue_create
function to create serial
queues. The first parameter in this function is a C string (char *
) that will uniquely identify that serial
queue in the system. The reason I am emphasizing
system is because this identifier is a system-wide
identifier, meaning that if your app creates a new serial queue with the
identifier of serialQueue1
and somebody else’s
app does the same, the results of creating a new serial queue with the
same name are undefined by GCD. Because of this, Apple strongly recommends
that you use a reverse DNS format for identifiers. Reverse DNS identifiers
are usually constructed in this way:
com.COMPANY
.PRODUCT
.IDENTIFIER
.
For instance, I could create two serial queues and assign these names to
them:
com.pixolity.GCD.serialQueue1 com.pixolity.GCD.serialQueue2
After you’ve created your serial queue, you can start dispatching
tasks to it using the various GCD
functions you’ve learned in this book. Once you are done with the
serial dispatch queue that you’ve just created, you
must dispose of it using the dispatch_release
function.
Would you like to see an example? I thought so!
dispatch_queue_t firstSerialQueue = dispatch_queue_create("com.pixolity.GCD.serialQueue1", 0); dispatch_async(firstSerialQueue, ^{ NSUInteger counter = 0; for (counter = 0; counter < 5; counter++){ NSLog(@"First iteration, counter = %lu", (unsigned long)counter); } }); dispatch_async(firstSerialQueue, ^{ NSUInteger counter = 0; for (counter = 0; counter < 5; counter++){ NSLog(@"Second iteration, counter = %lu", (unsigned long)counter); } }); dispatch_async(firstSerialQueue, ^{ NSUInteger counter = 0; for (counter = 0; counter < 5; counter++){ NSLog(@"Third iteration, counter = %lu", (unsigned long)counter); } }); dispatch_release(firstSerialQueue);
If you run this code and have a look at the output printed to the console window, you will see results similar to these:
First iteration, counter = 0 First iteration, counter = 1 First iteration, counter = 2 First iteration, counter = 3 First iteration, counter = 4 Second iteration, counter = 0 Second iteration, counter = 1 Second iteration, counter = 2 Second iteration, counter = 3 Second iteration, counter = 4 Third iteration, counter = 0 Third iteration, counter = 1 Third iteration, counter = 2 Third iteration, counter = 3 Third iteration, counter = 4
It’s obvious that although we dispatched our block objects
asynchronously to the serial queue, the queue has executed their code in a
FIFO fashion. We can modify the same sample code to make use of dispatch_async_f
function instead of the
dispatch_async
function, like
so:
void firstIteration(void *paramContext){ NSUInteger counter = 0; for (counter = 0; counter < 5; counter++){ NSLog(@"First iteration, counter = %lu", (unsigned long)counter); } } void secondIteration(void *paramContext){ NSUInteger counter = 0; for (counter = 0; counter < 5; counter++){ NSLog(@"Second iteration, counter = %lu", (unsigned long)counter); } } void thirdIteration(void *paramContext){ NSUInteger counter = 0; for (counter = 0; counter < 5; counter++){ NSLog(@"Third iteration, counter = %lu", (unsigned long)counter); } } - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ dispatch_queue_t firstSerialQueue = dispatch_queue_create("com.pixolity.GCD.serialQueue1", 0); dispatch_async_f(firstSerialQueue, NULL, firstIteration); dispatch_async_f(firstSerialQueue, NULL, secondIteration); dispatch_async_f(firstSerialQueue, NULL, thirdIteration); dispatch_release(firstSerialQueue); // Override point for customization after application launch. [self.window makeKeyAndVisible]; return YES; }