Skip to content

Instantly share code, notes, and snippets.

@thornpig
Created June 7, 2016 23:01
Show Gist options
  • Save thornpig/0fb70b85abcb84b8ad2d17a4c1056975 to your computer and use it in GitHub Desktop.
Save thornpig/0fb70b85abcb84b8ad2d17a4c1056975 to your computer and use it in GitHub Desktop.
Main queue and main thread in GCD
//The main queue must be associated with the main thread.
//The main thread is not exclusively associated with the main queue.
//For example, when disptach_sync is used to dispatch a task from queue A to queue B,
//queue B will use the same thread queue A is using to execute the task.
//So, if a task is dispatched to a non-main queue from the main queue with dispatch_sync,
//the non-main queue will use the main thread to execute the task.
//Check if a task is running on the main thread,
[NSThread isMianThread];
//Check if a task is running on the main queue,
// Associate a key-value pair with the main queue
dispatch_queue_set_specific(
dispatch_get_main_queue(),
mainQueueKey,
mainQueueValue,
nil
)
//use the key-value pair to do the check
if (dispatch_get_specific(mainQueueKey) == mainQueueValue)
{
NSLog(@"on the main queue");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment