Skip to content

Instantly share code, notes, and snippets.

@steipete
Created June 3, 2016 09:45
Show Gist options
  • Save steipete/41cba69f4df6754c111c0cb8c79fea5c to your computer and use it in GitHub Desktop.
Save steipete/41cba69f4df6754c111c0cb8c79fea5c to your computer and use it in GitHub Desktop.
Main thread checks
PSPDF_EXTERN BOOL PSPDFIsMainThread(void) {
// dispatch_get_current_queue is deprecated because most of the time it's not a good idea to call it.
// NSThread.isMainThread simply calls pthread_main_np so we can save a bit if we call it directly.
PSPDF_DEPRECATED_NOWARN(return pthread_main_np() || dispatch_get_current_queue() == dispatch_get_main_queue();)
@dtorres
Copy link

dtorres commented Jun 3, 2016

It is my impression this check does no more good than NSThread.isMainThread.

If pthread_main_np returns false, then the second will always be false as well because that queue only runs in the main thread.
on the other hand, If pthread_main_np returns false, the second condition will never run.

@yannickl
Copy link

yannickl commented Jun 3, 2016

And what do you think about this kind of condition:

dispatch_queue_get_label(dispatch_get_main_queue()) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment