swift - Parallel search with dispatch_async -
i'm trying implement parallel-search algorithm. concept this:
- start candidate & test if desired value
- if not, generate more candidates , add them queue.
- repeat until reaching desired value
as simplified example: want run random number generator in range 0..<n
until gives me 0. want decrease n
each iteration success guaranteed. code far:
let queue = dispatch_get_global_queue(qos_class_background, 0) let work : dispatch_function_t = { arg in let upper = unsafemutablepointer<uint32>(arg).memory let random = arc4random_uniform(upper) if random == 0 { // things } else { dispatch_async_f(queue, &(upper - 1), work) // error: variable used within own initial value } } dispatch_async_f(queue, &1000, work) // error: '&' used non inout argument of type 'unsafemutablepointer<void>'
i got 2 erros:
variable used within own initial value '&' used noninout argument of type 'unsafemutablepointer<void>'
how can fix them? many in advance!
you can fix "used within own initial value" doing declaration , initialization in 2 steps.
let work: dispatch_function_t work = { arg in let upper = unsafemutablepointer<uint32>(arg).memory let random = arc4random_uniform(upper) if random == 0 { // things } else { dispatch_async_f(queue, &(upper - 1), work) // error: variable used within own initial value } }
you can fix other 1 this.
var n = 1000 dispatch_async_f(dispatch_get_global_queue(qos_class_background, 0), &n, work)
Comments
Post a Comment