Last active
June 12, 2021 09:03
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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