Question

I have an Objective-C class which spins up a background thread and runs an NSRunLoop on it. I would like to deliver messages to the background thread from the main thread (yes, exclusively from the main thread). To do this, I am planning to use the built-in performSelector:onThread:withObject:waitUntilDone:.

I am wondering if it is safe to assume that the order in which I send messages this way will be the order in which they are received on the background thread. Obviously, the question is moot if multiple threads are calling performSelector:onThread:withObject:waitUntilDone: at nearly the same time, but since I will only be calling it from the main thread, am I guaranteed ordered queuing?

Was it helpful?

Solution

According to Apple's documentation:

This method queues the message on the run loop of the target thread using the default run loop modes—that is, the modes associated with the NSRunLoopCommonModes constant. As part of its normal run loop processing, the target thread dequeues the message (assuming it is running in one of the default run loop modes) and invokes the desired method.

A queue is typically an ordered data structure. The fact that it doesn't state that it could execute in any order implies that they will be executed in order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top