Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active September 16, 2020 08:37
Show Gist options
  • Save theoknock/2f0a48d3d058df301be56ac4fa9e65ca to your computer and use it in GitHub Desktop.
Save theoknock/2f0a48d3d058df301be56ac4fa9e65ca to your computer and use it in GitHub Desktop.
Passing context data via a custom dispatch source (basic)
// Prints 'x = 2.0' to the Console log
#import "custom_dispatch_source.h"
typedef struct ContextData
{
double x;
} ContextData;
@interface custom_dispatch_source ()
{
dispatch_queue_t dispatch_queue;
dispatch_source_t dispatch_source;
}
@end
@implementation custom_dispatch_source
- (void)dispatch_custom_source {
dispatch_queue = dispatch_queue_create("dispatch_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_queue);
dispatch_source_set_event_handler(dispatch_source, ^{
struct ContextData * data = dispatch_get_context(dispatch_source);
printf("x = %f", data->x);
});
dispatch_resume(dispatch_source);
struct ContextData *context_data = malloc(sizeof(struct ContextData));
context_data->x = 2.0;
dispatch_set_context(dispatch_source, context_data);
dispatch_source_merge_data(dispatch_source, 1);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment