Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active June 12, 2021 09:03
Show Gist options
  • Save theoknock/5ce48ffd8edb36b4de5d067a6d739e0a to your computer and use it in GitHub Desktop.
Save theoknock/5ce48ffd8edb36b4de5d067a6d739e0a to your computer and use it in GitHub Desktop.
Global dispatch queue (single instance) in C. Set a global queue once and use it anywhere the header file is imported.
#include "GlobalQueue.h"
dispatch_queue_t queue;
dispatch_queue_t queue_ref(void) {
if (!queue) {
queue = dispatch_queue_create_with_target("GlobalDispatchQueue", DISPATCH_QUEUE_SERIAL, dispatch_get_main_queue());
}
return queue;
}
#ifndef GlobalQueue_h
#define GlobalQueue_h
#include <stdio.h>
#include <dispatch/dispatch.h>
extern dispatch_queue_t queue;
extern dispatch_queue_t queue_ref(void);
#endif /* GlobalQueue_h */
// To use:
// 1. #include "GlobalQueue.h" in any .m or .c source file
// 2. Call queue_ref() to use the dispatch queue
// Example:
// dispatch_queue_t serial_queue_with_queue_target = dispatch_queue_create_with_target("serial_queue_with_queue_target", DISPATCH_QUEUE_SERIAL, queue_ref());
//
// Note:
// Once queue_ref() has been called, the queue can be used via the queue variable in all source files
// Example:
//
// Example:
// dispatch_queue_t serial_queue_with_queue_target = dispatch_queue_create_with_target("serial_queue_with_queue_target", DISPATCH_QUEUE_SERIAL, queue));
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment