A thread of execution.


@interface NSThread : NSObject


Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application, which handles user interface and event-related actions. Threads can also be used to divide a large job into several smaller jobs, which can lead to performance increases on multi-core computers.

The NSThread class supports semantics similar to those of NSOperation for monitoring the runtime condition of a thread. You can use these semantics to cancel the execution of a thread or determine if the thread is still executing or has finished its task. Canceling a thread requires support from your thread code; see the description for cancel for more information.

Subclassing Notes

You can subclass NSThread and override the main method to implement your thread’s main entry point. If you override main, you do not need to invoke the inherited behavior by calling super.


Initializing an NSThread Object

- init

Returns an initialized NSThread object.

- initWithTarget:selector:object:

Returns an NSThread object initialized with the given arguments.

Starting a Thread

+ detachNewThreadSelector:toTarget:withObject:

Detaches a new thread and uses the specified selector as the thread entry point.

- start

Starts the receiver.

- main

The main entry point routine for the thread.

Stopping a Thread

+ sleepUntilDate:

Blocks the current thread until the time specified.

+ sleepForTimeInterval:

Sleeps the thread for a given time interval.

+ exit

Terminates the current thread.

- cancel

Changes the cancelled state of the receiver to indicate that it should exit.

Determining the Thread’s Execution State


A Boolean value that indicates whether the receiver is executing.


A Boolean value that indicates whether the receiver has finished execution.


A Boolean value that indicates whether the receiver is cancelled.

Working with the Main Thread


Returns a Boolean value that indicates whether the current thread is the main thread.


A Boolean value that indicates whether the receiver is the main thread.


Returns the NSThread object representing the main thread.

Querying the Environment

+ isMultiThreaded

Returns whether the application is multithreaded.


Returns the thread object representing the current thread of execution.


Returns an array containing the call stack return addresses.


Returns an array containing the call stack symbols.

Working with Thread Properties


The thread object's dictionary.


The name of the receiver.


The stack size of the receiver, in bytes.

Prioritizing Thread Work


Used to indicate the nature and importance of work to the system. Work with higher quality of service classes receive more resources than work with lower quality of service classes whenever there is resource contention.

+ threadPriority

Returns the current thread’s priority.


The receiver’s priority

+ setThreadPriority:

Sets the current thread’s priority.



An NSThread object posts this notification when it receives the exit message, before the thread exits. Observer methods invoked to receive this notification execute in the exiting thread, before it exits.


Posted when the first thread is detached from the current thread. The NSThread class posts this notification at most once—the first time a thread is detached using detachNewThreadSelector:toTarget:withObject: or the start method. Subsequent invocations of those methods do not post this notification. Observers of this notification have their notification method invoked in the main thread, not the new thread. The observer notification methods always execute before the new thread begins executing.



Inherits From

See Also

Threads and Locking


The elementary methods adopted by classes that define lock objects.


An object that coordinates the operation of multiple threads of execution within the same application.


A lock that may be acquired multiple times by the same thread without causing a deadlock.


A lock that multiple applications on multiple hosts can use to restrict access to some shared resource, such as a file.


A lock that can be associated with specific, user-defined conditions.


A condition variable whose semantics follow those used for POSIX-style conditions.