ios - Making sure I'm explaining nested GCD correctly -
so i'm putting 10 tasks on concurrent queue using dispatch_async. not block next task, , gets processed in order. ui responsive.
for (int = 0; < 10; i++) { dispatch_async(concurrencyqueue, ^() { nslog(@"..calling insertion method insert record %d", i); dispatch_sync(serialqueue, ^() { //this simulate writing database nslog(@"----------start %d---------", i); [nsthread sleepfortimeinterval:1.0f]; nslog(@"--------finished %d--------", i); }); }); }
within each task, simulate write database "1 sec sleep" on serial queue via dispatch_sync.
i thought dispatch_sync blocks everyone, , syncs tasks because that's how behaves when use individually. however, in situation, not block main thread. instead, runs beautifully in background want it.
is because whatever thread associated queue being affected?
for example, main thread executing concurrent queue via dispatch_async , that's why not blocked.
the dispatch_sync syncs , blocks against background thread that's working on concurrent queue. hence, dispatch_sync associated background thread, never affecting ui main thread.
is thinking correct?
thank you!
you never block main thread because code running on either threads of concurrencyqueue
or thread of serialqueue
. none of main thread.
all of calls sleep
happen 1 one on thread of serialqueue
. thread of serialqueue
blocked.
however, since dispatch serialqueue
using dispatch_sync
, blocking each thread of concurrent queue. better pictured if add nslog
after call dispatch_sync
.
for (int = 0; < 10; i++) { dispatch_async(concurrencyqueue, ^() { nslog(@"..calling insertion method insert record %d", i); dispatch_sync(serialqueue, ^() { //this simulate writing database nslog(@"----------start %d---------", i); [nsthread sleepfortimeinterval:1.0f]; nslog(@"--------finished %d--------", i); }); nslog(@"..called insertion method insert record %d", i); }); }
that 2nd nslog
after dispatch_sync
show better how dispatch_sync
affecting calls dispatch_async
.
Comments
Post a Comment