Class

NSThread

A thread of execution.

Overview

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.

Topics

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

executing

A Boolean value that indicates whether the receiver is executing.

finished

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

cancelled

A Boolean value that indicates whether the receiver is cancelled.

Working with the Main Thread

isMainThread

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

isMainThread

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

mainThread

Returns the NSThread object representing the main thread.

Querying the Environment

isMultiThreaded

Returns whether the application is multithreaded.

currentThread

Returns the thread object representing the current thread of execution.

callStackReturnAddresses

Returns an array containing the call stack return addresses.

callStackSymbols

Returns an array containing the call stack symbols.

Working with Thread Properties

threadDictionary

The thread object's dictionary.

name

The name of the receiver.

stackSize

The stack size of the receiver, in bytes.

Prioritizing Thread Work

NSQualityOfService

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.

threadPriority

The receiver’s priority

setThreadPriority:

Sets the current thread’s priority.

Notifications

NSThreadWillExitNotification

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.

NSWillBecomeMultiThreadedNotification

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.

Initializers

Relationships

Inherits From

See Also

Threads and Locking

NSLocking

The elementary methods adopted by classes that define lock objects.

NSLock

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

NSRecursiveLock

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

NSDistributedLock

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

NSConditionLock

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

NSCondition

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