Skip to content

Instantly share code, notes, and snippets.

@rnapier rnapier/gist:7515273
Created Nov 17, 2013

Embed
What would you like to do?
Exploration of thread priorities vs queue priorities
// Main thread priorty = 0.758065
NSLog(@"main:%@:%f", [NSThread currentThread], [NSThread threadPriority]);
// High priority = 0.532258
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"high:%@:%f", [NSThread currentThread], [NSThread threadPriority]);
// run forever! Note that other queues still happily process, even though our high-priority block never ends
while(1) {}
});
// Default priorty = 0.5
dispatch_after(dispatch_time(0, NSEC_PER_SEC),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"default:%@:%f", [NSThread currentThread], [NSThread threadPriority]);
});
// Background priorty = 0.0
dispatch_after(dispatch_time(0, NSEC_PER_SEC),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSLog(@"background:%@:%f", [NSThread currentThread], [NSThread threadPriority]);
});
// Runs on the main thread (unsurprisingly)
// means it gets high priorty thread (since main is waiting for it)
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSLog(@"default(sync):%@:%f", [NSThread currentThread], [NSThread threadPriority]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.