Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dispatch_async_f?

The function that I want queued takes no parameters. What do I pass in as paramContext? Passing in NULL generates the compile error "Invalid use of void expression". I do not want to add a parameter to my function just to make it compile - how do I make this work?

Mac OS X Snowleopard, Xcode 3.2.6 with Objective-C

like image 460
fdh Avatar asked Feb 16 '12 22:02

fdh


People also ask

Does Dispatch_async create a thread?

When using dispatch_async for a background queue, GCD (Grand Central Dispatch) will ask the kernel for a thread, where the kernel either creates one, picks an idle thread, or waits for one to become idle. These threads, once created, live in the thread pool.

How does dispatch_ async work?

Submits a block for asynchronous execution on a dispatch queue and returns immediately.

What is Dispatch_get_main_queue?

An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread.

What is Dispatch_queue_t?

A dispatch queue that is bound to the app's main thread and executes tasks serially on that thread. dispatch_queue_global_t. A dispatch queue that executes tasks concurrently using threads from the global thread pool.


2 Answers

While you can just pass 0/NULL for the context argument, dispatch_async_f() takes void (*)(void*) as the function parameter, you can't pass it a function that takes no arguments.

You need to either change your function to take a void* parameter:

void func(void*) {}

... or, if you can't, wrap it:

void orig(void) {}
void wrapper(void*) { orig(); }

// ...
dispatch_async_f(queue, 0, &wrapper);
like image 139
Georg Fritzsche Avatar answered Sep 21 '22 00:09

Georg Fritzsche


You need to wrap the function somehow. The easiest way is actually to use dispatch_async() instead, as in

dispatch_async(queue, ^{ myFunc() });
like image 35
Lily Ballard Avatar answered Sep 18 '22 00:09

Lily Ballard