objective c - Implementing Critical section using GCD in iOS -


I have a very heavy job, and I do not want to block the main thread. That's why I'm starting a separate concurrent quote for this can be 4 examples of work.

  - (dispatch_queue_t) getConcurrentQueue {if (concurrent questions == zero) {concurrentQueue = dispatch_queue_create ("com.myself.HeavyTask", DISPATCH_QUEUE_CONCURRENT); } Return concurrent queue; }   

Now to start a heavy work with me -

  - (zero) start.tatas {// ..... // .. ... __Week type (self) weak Self = self; Dispatch_QI_T queue = [quiescupe calculator]; Dispatches_assink (Qatar, ^ {[weak selfie photo};}); }   

Now the method is heavy tasks like this -

  - (zero) heavy task {// ... dispatch_sync (dispatch_get_current_queue (), ^ {// To start the code for heavy work // This is important section 4. Only one of the concurrent threads can enter // .... while (position) {// display the meat of work } // ... Dispatch_asisk (sending_reference), ^ {// tidown code.freeing memory etc. It is also an important section.} // ...}   

Initialization code and T The code uses some third-party C methods that are not secure thread, so making them secure the thread is not the question of the question.

Now I have started the code And within "teardown code"

  sender_sINCC (sending_gate_computer), ^ {}   

my code is crashing And I'm getting this error message that insufficient threads around important section code

I have read that transmissions_small () is not secure, so I replaced it with the concurrent quote . I also tried to replace with dispatch_get_main_queue () . Still the code is crashing, there is a complaint about inadequate thread locking.

I know that there is something wrong with my understanding of implementing important sections using GCD.

Can anyone show me clearly how to do your code properly ??

Side query - Can I use the @synchronized {} block

There is a lot of confusion in your code, including not following naming conventions

If you want to perform similar tasks with respect to the second simultaneously , then use the global concurrent queue to perform those tasks.

If you want to use shared resources from within these actions (or from anywhere), define a dedicated queue, say "sync_queue", where you specifically use these resources . This "sync_queue" performs your "critical sections".

"sync_queue" can be serial or concurrent.

If you use a serial queue, to write and for dispatch_sync (sync_queue, block), sending_config, block < / code> Use

for sharing resources If you use a concurrent queue read to use write and dispatch_barrier_sync (sync_queue, block) for share resources, reader_barre_accommon (sync_queue, block) Example for

:

  Read using // Serial sync_queue: ... __block Int counter; Dispatch_sync (sync_queue, ^ {counter = _counter;}); // Write a serial using sync_queue: ... sending_sync (sync_queue, ^ {_counter = counter;}); // read concurrently using the sync_queue: ... __block int counter; Dispatch_barrier_sync (sync_queue, ^ {counter = _counter;}); // Write a concurrent using sync_queue ...: dispatch_barrier_async (sync_queue, ^ {_counter = counter;}); Example for your "heavy work":  
  - (zero) heavy task {dispatch_barrier_async (sync_queue, ^ {// start heavy work ... / / continue with work: dispatch_async (Dispatch_get_global_queue (0,0), ^ {BOOL condition = yes; // condition should be local in block (this is not shared resource!) While (condition) {// meat of the performance work status = ...;} Dispatch_barar_asic (sync_queue, ^ {// tidown code.freeing memory etc.) is also an important section ... ...}}}}}    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -