Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads that don’t have a runloop

I'm reading Core Animation Programming Guide and in the chapter of "Transactions", I see this

Important: When modifying layer properties from threads that don’t have a runloop, you must use explicit transactions.

but From Apple's documentation on NSRunLoop

Your application cannot either create or explicitly manage NSRunLoop objects. Each NSThread object, including the application’s main thread, has an NSRunLoop object automatically created for it as needed.

Doesn't it mean every thread has a runloop? or threads that's not created by NSThread, such as POSIX's pthread

like image 908
keywind Avatar asked Jan 15 '23 14:01

keywind


1 Answers

It says “Each NSThread object, including the application’s main thread, has an NSRunLoop object automatically created for it as needed.”

If you don't do anything that tries to access a thread's run loop, the system won't create a run loop for the thread.

If you don't do [[NSRunLoop currentRunLoop] run] (or something equivalent), your thread won't run its run loop.

The UIApplicationMain function takes care of this for the main thread. For threads you create, you need to run the thread's run loop if you want the thread's run loop to have any effect.

Here's what's happening (I think) in the case of Core Animation, when you don't use an explicit transaction. It begins a transaction, and registers a callback with the current thread's run loop to commit it. (This will create a run loop for the current thread if necessary.) If you're not running the thread's run loop, that callback will never be called.

like image 57
rob mayoff Avatar answered Jan 22 '23 09:01

rob mayoff