Up

NSThread class reference

Authors

Scott Christley (scottc@net-community.com)
Andrew Kachites McCallum (mccallum@gnu.ai.mit.edu)
Richard Frith-Macdonald (richard@brainstorm.co.uk)
Nicola Pero (n.pero@mi.flashnet.it)

Version: 38791

Date: 2015-07-14 12:05:55 -0600 (Tue, 14 Jul 2015)

Copyright: (C) 1996-2000 Free Software Foundation, Inc.


Contents -

  1. Software documentation for the NSThread class
  2. Software documentation for the NSObject(NSThreadPerformAdditions) category
  3. Software documentation for the NSThread(CallStackSymbols) category

Software documentation for the NSThread class

NSThread : NSObject

Declared in:
Foundation/NSThread.h
Availability: OpenStep

This class encapsulates OpenStep threading. See NSLock and its subclasses for handling synchronisation between threads.
Each process begins with a main thread and additional threads can be created using NSThread. The GNUstep implementation of OpenStep has been carefully designed so that the internals of the base library do not use threading (except for methods which explicitly deal with threads of course) so that you can write applications without threading. Non-threaded applications are more efficient (no locking is required) and are easier to debug during development.

Instance Variables

Method summary

callStackReturnAddresses 

+ (NSArray*) callStackReturnAddresses;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns an array of the call stack return addresses.

currentThread 

+ (NSThread*) currentThread;
Availability: OpenStep

Returns the NSThread object corresponding to the current thread.

NB. In GNUstep the library internals use the GSCurrentThread() function as a more efficient mechanism for doing this job - so you cannot use a category to override this method and expect the library internals to use your implementation.


detachNewThreadSelector: toTarget: withObject: 

+ (void) detachNewThreadSelector: (SEL)aSelector toTarget: (id)aTarget withObject: (id)anArgument;
Availability: OpenStep

Create a new thread - use this method rather than alloc-init. The new thread will begin executing the message given by aSelector, aTarget, and anArgument. This should have no return value, and must set up an autorelease pool if retain/release memory management is used. It should free this pool before it finishes execution.


exit 

+ (void) exit;
Availability: OpenStep

Terminates the current thread.
Normally you don't need to call this method explicitly, since exiting the method with which the thread was detached causes this method to be called automatically.

isMainThread 

+ (BOOL) isMainThread;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns a boolean indicating whether this thread is the main thread of the process.

isMultiThreaded 

+ (BOOL) isMultiThreaded;
Availability: OpenStep

Returns a flag to say whether the application is multi-threaded or not.
An application is considered to be multi-threaded if any thread other than the main thread has been started, irrespective of whether that thread has since terminated.
NB. This method returns YES if called within a handler processing NSWillBecomeMultiThreadedNotification

mainThread 

+ (NSThread*) mainThread;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns the main thread of the process.

setThreadPriority: 

+ (void) setThreadPriority: (double)pri;
Availability: MacOS-X 10.2.0, Base 1.2.0

Set the priority of the current thread. This is a value in the range 0.0 (lowest) to 1.0 (highest) which is mapped to the underlying system priorities.

sleepForTimeInterval: 

+ (void) sleepForTimeInterval: (NSTimeInterval)ti;
Availability: MacOS-X 10.5.0, Base 1.15.1

Suspends execution of the process for the specified period.

sleepUntilDate: 

+ (void) sleepUntilDate: (NSDate*)date;
Availability: OpenStep

Delaying a thread... pause until the specified date.

threadPriority 

+ (double) threadPriority;
Availability: MacOS-X 10.2.0, Base 1.2.0

Return the priority of the current thread.

cancel 

- (void) cancel;
Availability: MacOS-X 10.5.0, Base 1.15.1

Cancels the receiving thread.

init 

- (id) init;
Availability: MacOS-X 10.5.0, Base 1.15.1

This is a designated initialiser for the class.
Description forthcoming.

initWithTarget: selector: object: 

- (id) initWithTarget: (id)aTarget selector: (SEL)aSelector object: (id)anArgument;
Availability: MacOS-X 10.5.0, Base 1.15.1

Initialises the receiver to send the message aSelector to the object aTarget with the argument anArgument (which may be nil).
The arguments aTarget and aSelector are retained while the thread is running.

isCancelled 

- (BOOL) isCancelled;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns a boolean indicating whether the receiving thread has been cancelled.

isExecuting 

- (BOOL) isExecuting;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns a boolean indicating whether the receiving thread has been started (and has not yet finished or been cancelled).

isFinished 

- (BOOL) isFinished;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns a boolean indicating whether the receiving thread has completed executing.

isMainThread 

- (BOOL) isMainThread;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns a boolean indicating whether this thread is the main thread of the process.

main 

- (void) main;
Availability: MacOS-X 10.5.0, Base 1.15.1

FIXME... what does this do?

name 

- (NSString*) name;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns the name of the receiver.

setName: 

- (void) setName: (NSString*)aName;
Availability: MacOS-X 10.5.0, Base 1.15.1

Sets the name of the receiver.

setStackSize: 

- (void) setStackSize: (NSUInteger)stackSize;
Availability: MacOS-X 10.5.0, Base 1.15.1

Sets the size of the receiver's stack.

stackSize 

- (NSUInteger) stackSize;
Availability: MacOS-X 10.5.0, Base 1.15.1

Returns the size of the receiver's stack.

start 

- (void) start;
Availability: MacOS-X 10.5.0, Base 1.15.1

Starts the receiver executing.

threadDictionary 

- (NSMutableDictionary*) threadDictionary;
Availability: OpenStep

Return the thread dictionary. This dictionary can be used to store arbitrary thread specific data.
NB. This cannot be autoreleased, since we cannot be sure that the autorelease pool for the thread will continue to exist for the entire life of the thread!



Instance Variables for NSThread Class

_active

@public BOOL _active;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_arg

@public id _arg;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_autorelease_vars

@public struct autorelease_thread_vars _autorelease_vars;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_cancelled

@public BOOL _cancelled;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_exception_handler

@public NSHandler* _exception_handler;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_finished

@public BOOL _finished;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_gcontext

@public id _gcontext;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_name

@public NSString* _name;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_runLoopInfo

@public void* _runLoopInfo;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_selector

@public SEL _selector;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_stackSize

@public NSUInteger _stackSize;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_target

@public id _target;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

_thread_dictionary

@public NSMutableDictionary* _thread_dictionary;
Availability: OpenStep

Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.




Software documentation for the NSObject(NSThreadPerformAdditions) category

NSObject(NSThreadPerformAdditions)

Declared in:
Foundation/NSThread.h
Availability: OpenStep

Extra methods to permit messages to be sent to an object such that they are executed in another thread.
The main thread is the thread in which the GNUstep system is started, and where the GNUstep gui is used, it is the thread in which gui drawing operations must be performed.
Method summary

performSelector: onThread: withObject: waitUntilDone: 

- (void) performSelector: (SEL)aSelector onThread: (NSThread*)aThread withObject: (id)anObject waitUntilDone: (BOOL)aFlag;
Availability: Base 10.5.0

Invokes -performSelector:onThread:withObject:waitUntilDone:modes: using the supplied arguments and an array containing common modes.
These modes consist of NSRunLoopMode, NSConnectionreplyMode, and if in an application, the NSApplication modes.

performSelector: onThread: withObject: waitUntilDone: modes: 

- (void) performSelector: (SEL)aSelector onThread: (NSThread*)aThread withObject: (id)anObject waitUntilDone: (BOOL)aFlag modes: (NSArray*)anArray;
Availability: Base 10.5.0

This method performs aSelector on the receiver, passing anObject as an argument, but does so in the specified thread. The receiver and anObject are both retained until the method is performed.

The selector is performed when the runloop of aThread next runs in one of the modes specified in anArray.
Where this method has been called more than once before the runloop of the thread runs in the required mode, the order in which the operations in the thread is done is the same as that in which they were added using this method.

If there are no modes in anArray, the method has no effect and simply returns immediately.

The argument aFlag specifies whether the method should wait until the selector has been performed before returning.
NB. This method does not cause the runloop of aThread to be run... so if the runloop is not executed by some code in aThread, the thread waiting for the perform to complete will block forever.

As a special case, if aFlag == YES and the current thread is aThread, the modes array is ignored and the selector is performed immediately. This behavior is necessary to avoid the current thread being blocked by waiting for a perform which will never happen because the runloop is not executing.


performSelectorInBackground: withObject: 

- (void) performSelectorInBackground: (SEL)aSelector withObject: (id)anObject;
Availability: Base 10.5.0

Creates and runs a new background thread sending aSelector to the receiver and passing anObject (which may be nil) as the argument.

performSelectorOnMainThread: withObject: waitUntilDone: 

- (void) performSelectorOnMainThread: (SEL)aSelector withObject: (id)anObject waitUntilDone: (BOOL)aFlag;
Availability: Base 10.2.0

Invokes -performSelectorOnMainThread:withObject:waitUntilDone:modes: using the supplied arguments and an array containing common modes.
These modes consist of NSRunLoopMode, NSConnectionReplyMode, and if in an application, the NSApplication modes.

performSelectorOnMainThread: withObject: waitUntilDone: modes: 

- (void) performSelectorOnMainThread: (SEL)aSelector withObject: (id)anObject waitUntilDone: (BOOL)aFlag modes: (NSArray*)anArray;
Availability: Base 10.2.0

This method performs aSelector on the receiver, passing anObject as an argument, but does so in the main thread of the program. The receiver and anObject are both retained until the method is performed.

The selector is performed when the runloop of the main thread next runs in one of the modes specified in anArray.
Where this method has been called more than once before the runloop of the main thread runs in the required mode, the order in which the operations in the main thread is done is the same as that in which they were added using this method.

If there are no modes in anArray, the method has no effect and simply returns immediately.

The argument aFlag specifies whether the method should wait until the selector has been performed before returning.
NB. This method does not cause the runloop of the main thread to be run... so if the runloop is not executed by some code in the main thread, the thread waiting for the perform to complete will block forever.

As a special case, if aFlag == YES and the current thread is the main thread, the modes array is ignored and the selector is performed immediately. This behavior is necessary to avoid the main thread being blocked by waiting for a perform which will never happen because the runloop is not executing.


Software documentation for the NSThread(CallStackSymbols) category

NSThread(CallStackSymbols)

Declared in:
Foundation/NSThread.h
Availability: OpenStep

Description forthcoming.
Method summary

callStackSymbols 

+ (NSArray*) callStackSymbols;
Availability: Base 10.6.0

Returns an array of NSString objects representing the current stack in an implementation-defined format. May return an empty array if this feature is not available.


Up