ios - dispatch task into queue in order to run the task in another thread -


this question has answer here:

people asking what way run task in thread. 1 of answer accepted many people(from link) use gcd.

so, tried in way printing out thread id see if task executed in thread after put gcd queue:

// function in myservice class     - (void) dotask {         nslog(@"start task on thread = %@", [nsthread currentthread]);         dispatch_queue_t queue = dispatch_queue_create("com.company.myqueue", dispatch_queue_serial);         dispatch_sync(queue, ^{             nslog(@"execute task on thread = %@", [nsthread currentthread]);         });     } 

i run :

// not main thread [myservice dotask]

the console output this:

start task on thread = <nsthread: 0x15b4be00>{number = 6, name = (null)} execute task on thread = <nsthread: 0x15b4be00>{number = 6, name = (null)} 

it looks task not executed in thread, on same thread caller thread. misunderstand something? why gcd doesn't execute task in separate thread many pepole accept answer in link?

gcd optimizes when can. running block synchronously never requires switching threads, sole exception of dispatching main queue, must run on main thread.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -