multithreading - iOS - Process photo on multiple threads -


i wondering if knew of straight forward way run x amount of threads concurrently in ios using swift. trying split photo multiple sections , analyze them concurrently. have looked , there lot of talk , examples using main thread , background thread, can't seem find examples run more 2 threads. example in java do:

public class concurrentanalysis implements runnable {     private static volatile int counter = 0;      public void run()     {         // increment count 2         counter += 2;     }      public static void main(string args[])     {         thread thread1 = new thread();         thread thread2 = new thread();          thread1.start();         thread2.start();          try         {             thread1.join();             thread2.join();         }         catch (interruptedexception e){ system.out.println("exception thrown: " + e.getmessage()); }     } } 

i know used 2 threads in example, have added many threads wanted. trying similar above java code in swift.

1. don't use thread directly. there better solutions:

there great library working gcd - async
nsoperationqueue - great when need control order of executed operation.

2. don't share mutable data between threads

if need synchronisation mechanism like: locks, mutex.
hard work wit architecture.

3. use immutable data structure
immutable data structures safe work in multithread because no 1 can't mutate them can safely work data (read) in many thread simultaneously.

structs in swift immutable value types great solution multithreading.

better solution

the thread input data, process , return result.
way make thread undefended , can press same image simultaneously.

example:

class multithread {    func processimage(image: uiimage, result:(uiimage -> void) ) {      dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0)) {        //do want image because uiimage immutable       //do work image       var resultimage = image       let data = resultimage.cgimage        //pass result calling thread, main threa in our example       dispatch_async(dispatch_get_main_queue()) {         //assign new image         result(resultimage)       }     }   } }  // use example  if let image = uiimage(named: "image") {   let worker = multithread()    //run multiple workers.   worker.processimage(image) { resultimage in     //result     println(resultimage)   }   worker.processimage(image) { resultimage in     //result     println(resultimage)   }   worker.processimage(image) { resultimage in     //result     println(resultimage)   }   worker.processimage(image) { resultimage in     //result     println(resultimage)   } } 

here how processimage if use async framework:

func processimage(image: uiimage, result:(uiimage -> void) ) {    async.background {     //do want image because uiimage immutable     //do work image     var resultimage = image     let data = resultimage.cgimage      //pass result calling thread, main threa in our example     async.main {       //assign new image       result(resultimage)     }   } } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -