Paste number 86524: simple CFRunLoop example

Index of paste annotations: 1

Paste number 86524: simple CFRunLoop example
Pasted by: toby
When:2 years, 8 months ago
Share:Tweet this! | http://paste.lisp.org/+1URG
Channel:#opendarwin
Paste contents:
Raw Source | XML | Display As
#include <CoreFoundation/CoreFoundation.h>

static void
_perform(void *info __unused)
{
	printf("hello\n");
}

static void
_timer(CFRunLoopTimerRef timer __unused, void *info)
{
	CFRunLoopSourceSignal(info);
}

int
main()
{
	CFRunLoopSourceRef source;
	CFRunLoopSourceContext source_context;
	CFRunLoopTimerRef timer;
	CFRunLoopTimerContext timer_context;

	bzero(&source_context, sizeof(source_context));
	source_context.perform = _perform;
	source = CFRunLoopSourceCreate(NULL, 0, &source_context);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);

	bzero(&timer_context, sizeof(timer_context));
	timer_context.info = source;
	timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent(), 1, 0, 0, _timer, &timer_context);
	CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);

	CFRunLoopRun();

	return 0;
}

Annotations for this paste:

Annotation number 1: libdispatch equivalent
Pasted by: toby
When:2 years, 8 months ago
Share:Tweet this! | http://paste.lisp.org/+1URG/1
Paste contents:
Raw Source | Display As
#include <dispatch/dispatch.h>
#include <stdio.h>

int
main()
{
	dispatch_source_t source, timer;

	source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
	dispatch_source_set_event_handler(source, ^{
		printf("hello\n");
	});
	dispatch_resume(source);

	timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
	dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1ull * NSEC_PER_SEC, 0);
	dispatch_source_set_event_handler(timer, ^{
		dispatch_source_merge_data(source, 1);
	});
	dispatch_resume(timer);

	dispatch_main();
}

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.