Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if dispatch_main gets called from outside of the main thread?

The dispatch_main function is used to make the main thread start processing blocks dispatched to the main queue. So, dispatch_main is a kind of run loop, which doesn't return and, after processing the already-queued blocks, waits for other blocks to be submitted to the main queue.

So, what happens if dispatch_main gets called from outside of the main thread? If the main thread is processing another function, is it interrupted in order to allow the main thread to process the queued blocks? Is it allowed to call dispatch_main from outside of the main thread?

like image 432
LuisABOL Avatar asked Feb 17 '23 13:02

LuisABOL


1 Answers

dispatch_main() asserts when called from outside of the main thread and aborts your process, it must only be called from the main thread.

dispatch_main() is really nothing other than pthread_exit() in disguise (see implementation): it turns the main queue into an ordinary serial dispatch queue and then terminates the main thread.

The main queue will be serviced by an on-demand workqueue thread from that point on, just like any other dispatch queue.

like image 79
das Avatar answered May 07 '23 08:05

das