4

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

Community
  • 1
  • 1
keywind
  • 1,135
  • 14
  • 24
  • 2
    Every *NSThread* has a run loop, but you could also create threads in C, I suppose. – borrrden Jan 04 '13 at 06:27
  • If you are using pthreads, you might look over this answer to a related question: http://stackoverflow.com/questions/4930957/nsstream-and-sockets-nsstreamdelegate-methods-not-being-called – Brandon Apr 16 '13 at 18:54

1 Answers1

9

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • "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." - It would be very useful if you could share some link to the documentation which explains this part. Or maybe it's your guess? – Legonaftik Feb 05 '21 at 15:46
  • The original question quotes the relevant `NSRunLoop` documentation, and the first line of my answer quotes it again. I'm not sure what else there is to say. You are also welcome to examine [the `CFRunLoopGetCurrent` source code](https://github.com/apple/swift-corelibs-foundation/blob/931dd5f14ffc627b02cc240f23fc51d15ff95cf1/CoreFoundation/RunLoop.subproj/CFRunLoop.c#L1573) for yourself. – rob mayoff Feb 05 '21 at 16:07