Skip to content

Instantly share code, notes, and snippets.

@janodev
Created November 8, 2012 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janodev/4041078 to your computer and use it in GitHub Desktop.
Save janodev/4041078 to your computer and use it in GitHub Desktop.
Running code on a specified queue
// queue name
static const char* s_myqueue = "myqueue";
// return the specific queue
dispatch_queue_t my_queue() {
static dispatch_queue_t _q;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_q = dispatch_queue_create(s_myqueue, 0);
// sets the key/value data for the specified dispatch queue
dispatch_queue_set_specific(_q, // queue
s_myqueue, // context key (only compared as pointer)
(void*) s_myqueue, // context data
NULL); // destructor for the context data
});
return _q;
}
static int foo()
{
// returns the value for the key associated with the current dispatch queue
if (dispatch_get_specific(s_myqueue) == s_myqueue) {
// we are running on the queue created at my_queue() so just do the work
return do_foo();
}
// dispath sync to the my_queue
__block int result;
dispatch_sync(my_queue(), ^{
result = do_foo();
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment