swift - Parallel search with dispatch_async -


i'm trying implement parallel-search algorithm. concept this:

  1. start candidate & test if desired value
  2. if not, generate more candidates , add them queue.
  3. 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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -