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. Now to start a heavy work with me - Now the method is heavy tasks like this - 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 my code is crashing And I'm getting this error message that insufficient threads around important section code I have read that 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 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 sharing resources If you use a concurrent queue read to use write and :
- (dispatch_queue_t) getConcurrentQueue {if (concurrent questions == zero) {concurrentQueue = dispatch_queue_create ("com.myself.HeavyTask", DISPATCH_QUEUE_CONCURRENT); } Return concurrent queue; }
- (zero) start.tatas {// ..... // .. ... __Week type (self) weak Self = self; Dispatch_QI_T queue = [quiescupe calculator]; Dispatches_assink (Qatar, ^ {[weak selfie photo};}); }
- (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.} // ...}
And
within "teardown code"
sender_sINCC (sending_gate_computer), ^ {}
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.
@synchronized {} block
for dispatch_sync (sync_queue, block),
sending_config, block < / code> Use
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
Post a Comment