| Framework | CoreServices/CoreServices.h |
| Declared in | Threads.h |
You can use the Thread Manager to provide cooperatively scheduled threads, or multiple points of execution, in an application. You can think of the Thread Manager as an enhancement to the classic Mac OS Process Manager, which governs how applications work together in the Mac OS cooperative multitasking environment.
Important: Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Consider using the Thread Manager for applications with more than one thread if these threads can execute only in the cooperative multitasking environment of the classic Mac OS Process Manager.
Alternatively, you should consider using the Multiprocessing Services to implement separate paths of execution for tasks that are reentrant and can therefore can be preemptively scheduled.
Using Thread Manager routines, you can create threads and thread pools and set them up to run; turn scheduling on and off; work with stacks; create dialog boxes that yield control to other threads; pass information between threads; install custom scheduling and context-switching functions; and use threads to make asynchronous I/O calls.
The Thread Manager provides only cooperative threading for PowerPC applications. Applications can use the Multiprocessing Services API to create preemptively scheduled tasks.
Note that several Thread Manager functions that did not require you to pass universal procedure pointers (UPPs) for callbacks now require them in Carbon. See the Carbon Porting Notes for more information.
CreateThreadPool
GetDefaultThreadStackSize
GetFreeThreadCount Deprecated in Mac OS X v10.3
GetSpecificFreeThreadCount Deprecated in Mac OS X v10.3
DisposeDebuggerDisposeThreadUPP
DisposeDebuggerNewThreadUPP
DisposeDebuggerThreadSchedulerUPP
DisposeThreadEntryUPP
DisposeThreadSchedulerUPP
DisposeThreadSwitchUPP
DisposeThreadTerminationUPP
InvokeDebuggerDisposeThreadUPP
InvokeDebuggerNewThreadUPP
InvokeDebuggerThreadSchedulerUPP
InvokeThreadEntryUPP
InvokeThreadSchedulerUPP
InvokeThreadSwitchUPP
InvokeThreadTerminationUPP
NewDebuggerDisposeThreadUPP
NewDebuggerNewThreadUPP
NewDebuggerThreadSchedulerUPP
NewThreadEntryUPP
NewThreadSchedulerUPP
NewThreadSwitchUPP
NewThreadTerminationUPP
Creates a pool of threads for your application.
OSErr CreateThreadPool ( ThreadStyle threadStyle, SInt16 numToCreate, Size stackSize );
The type of thread to create for this set of threads in the pool. Cooperative is the only type that you can specify. Historically, the Thread Manger supported two types of threads, preemptive and cooperative. However, due to severe limitations on their use, the Thread Manager no longer supports preemptive threads.
The number of threads to create for the pool.
The stack size for this set of threads in the pool. This stack must be large enough to handle saved thread context, normal application stack usage, interrupt handling functions, and CPU exceptions. Specify a stack size of 0 to request the Thread Manager’s default stack size for the specified type of thread.
A result code. See “Thread Manager Result Codes.”
The CreateThreadPool function creates the specified number of threads with the specified stack requirements. It places the threads that it creates into a pool for use by your application.
When you call CreateThreadPool, if the Thread Manager is unable to create all the threads that you specify, it does not create any at all and returns the memFullErr result code.
The threads in the pool are indistinguishable except by stack size. That is, you cannot refer to them individually. When you want to use a thread to execute some code in your application, you allocate a thread of a specific size from the pool using the NewThread function. The NewThread function assigns a thread ID to the thread and specifies the function that is the entry point to the thread.
Note that it is not strictly necessary to create a pool of threads before allocating a thread. If you wish, you can use the NewThread function to create and allocate a thread in one step. The advantage of using CreateThreadPool is that you can allocate memory for threads early in your application’s execution before memory is used or fragmented.
Before making any calls to CreateThreadPool, be certain that you first have called the Memory Manager function MaxApplZone to extend the application heap to its limit. You must call MaxApplZone from the main application thread before any other threads in your application run.
To allocate a thread from the pool created with CreateThreadPool, use the NewThread function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeDebuggerDisposeThreadUPP ( DebuggerDisposeThreadUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeDebuggerNewThreadUPP ( DebuggerNewThreadUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeDebuggerThreadSchedulerUPP ( DebuggerThreadSchedulerUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Deletes a thread when it finishes executing.
OSErr DisposeThread ( ThreadID threadToDump, void *threadResult, Boolean recycleThread );
The thread ID of the thread to delete.
A pointer to the thread’s result. The DisposeThread function places this result to an address which you originally specify with the threadResult parameter of the NewThread function when you create or allocate the thread. Pass a value of NULL if you are not interested in obtaining a function result.
A Boolean value that specifies whether to return the thread to the allocation pool or to remove it entirely. Specify False to dispose of the thread entirely and True to return it to the thread pool.
A result code. See “Thread Manager Result Codes.”
When a thread finishes executing, the Thread Manager automatically calls DisposeThread to delete it. Therefore, the only reason for you to explicitly call DisposeThread is to recycle a terminating thread. To do so, set the recycleThread parameter to True. The Thread Manager clears out the thread’s internal data structure, resets it, and puts the thread in the thread pool where it can be used again as necessary.
The DisposeThread function sets the threadResult parameter to the thread’s function result. You allocate the storage for the thread result when you create or allocate a thread with the NewThread function.
You cannot explicitly dispose of the main application thread. If you attempt to do so, DisposeThread returns the threadProtocolErr result code.
When your application terminates, the Thread Manager calls DisposeThread to terminate any active threads. It terminates stopped and ready threads first but in no special order. It terminates the currently running thread last. This thread should always be the main application thread.
To install a callback function to do special cleanup when a thread terminates, use the SetThreadTerminator function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeThreadEntryUPP ( ThreadEntryUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeThreadSchedulerUPP ( ThreadSchedulerUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeThreadSwitchUPP ( ThreadSwitchUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void DisposeThreadTerminationUPP ( ThreadTerminationUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Obtains the thread ID of the currently executing thread.
OSErr GetCurrentThread ( ThreadID * currentThreadID );
On return, a pointer to the thread ID of the current thread.
A result code. See “Thread Manager Result Codes.”
You can use the thread ID obtained by GetCurrentThread in functions such as GetThreadState and SetThreadState to get and set the state of a thread.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Determines the default stack size required by a thread.
OSErr GetDefaultThreadStackSize ( ThreadStyle threadStyle, Size *stackSize );
The type of thread to get information about. Cooperative is the only type that you can specify. Historically, the Thread Manger supported two types of threads, preemptive and cooperative, but the Thread Manager no longer supports preemptive threads.
On return, a pointer to the default stack size (in bytes). When you create a thread pool or an individual thread, this is the stack size that the Thread Manager allocates when you specify the default size.
A result code. See “Thread Manager Result Codes.”
Keep in mind that the default stack size is not an absolute value that you must use but is a rough estimate.
To determine how much stack space is available for a particular thread, use the ThreadCurrentStackSpace function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Obtains a thread task reference.
OSErr GetThreadCurrentTaskRef ( ThreadTaskRef *threadTRef );
On return, a pointer to a thread task reference.
A result code. See “Thread Manager Result Codes.”
The thread task reference is somewhat of a misnomer because it identifies your application context, not a particular thread. Identifying your application context is necessary in situations where you aren’t guaranteed that your application is the current context—such as during the execution of an interrupt function. In such cases, you need both the thread ID to identify the thread and the thread task reference to identify the application context.
After you obtain the thread task reference, you can use it in the GetThreadStateGivenTaskRef and SetThreadReadyGivenTaskRef functions to get and set information about specific threads in your application at times when you are not guaranteed that your application is the current context.
To get information about a thread when your application is not the current process, use the GetThreadStateGivenTaskRef function.
To change the state of a thread from stopped to ready when your application is not the current process, use the SetThreadReadyGivenTaskRef function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Obtains the state of a thread.
OSErr GetThreadState ( ThreadID threadToGet, ThreadState *threadState );
The thread ID of the thread about which you want information.
On return, a pointer to the state of the thread specified by threadToGet.
A result code. See “Thread Manager Result Codes.”
A thread can be in one of three states: ready to execute ( kReadyThreadState), stopped ( kStoppedThreadState), or executing ( kRunningThreadState).
To change the state of a specified thread, use SetThreadState.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Obtains the state of a thread when your application is not necessarily the current process—for example, during execution of an interrupt function.
OSErr GetThreadStateGivenTaskRef ( ThreadTaskRef threadTRef, ThreadID threadToGet, ThreadState *threadState );
The thread task reference of the application containing the thread whose state you want to determine.
The thread ID of the thread whose state you want to determine.
A pointer to a thread state variable in which the function places the state of the specified thread.
A result code. See “Thread Manager Result Codes.”
You can use GetThreadStateGivenTaskRef at times when you aren’t guaranteed that your application is the current context, such as during execution of an interrupt function. In such cases you must identify the thread task reference (the application context) as well as the thread ID.
To determine the thread task reference (application context) for your application, use the GetThreadCurrentTaskRef function.
To change the state of a thread from stopped to ready when your application is not the current process, use the SetThreadReadyGivenTaskRef function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void InvokeDebuggerDisposeThreadUPP ( ThreadID threadDeleted, DebuggerDisposeThreadUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void InvokeDebuggerNewThreadUPP ( ThreadID threadCreated, DebuggerNewThreadUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
ThreadID InvokeDebuggerThreadSchedulerUPP ( SchedulerInfoRecPtr schedulerInfo, DebuggerThreadSchedulerUPP userUPP );
See the description of the ThreadID data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
voidPtr InvokeThreadEntryUPP ( void *threadParam, ThreadEntryUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
ThreadID InvokeThreadSchedulerUPP ( SchedulerInfoRecPtr schedulerInfo, ThreadSchedulerUPP userUPP );
See the description of the ThreadID data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void InvokeThreadSwitchUPP ( ThreadID threadBeingSwitched, void *switchProcParam, ThreadSwitchUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
void InvokeThreadTerminationUPP ( ThreadID threadTerminated, void *terminationProcParam, ThreadTerminationUPP userUPP );
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
DebuggerDisposeThreadUPP NewDebuggerDisposeThreadUPP ( DebuggerDisposeThreadProcPtr userRoutine );
See the description of the DebuggerDisposeThreadUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
DebuggerNewThreadUPP NewDebuggerNewThreadUPP ( DebuggerNewThreadProcPtr userRoutine );
See the description of the DebuggerNewThreadUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
DebuggerThreadSchedulerUPP NewDebuggerThreadSchedulerUPP ( DebuggerThreadSchedulerProcPtr userRoutine );
See the description of the DebuggerThreadSchedulerUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Creates a new thread or allocates one from the existing pool of threads.
Modified
OSErr NewThread ( ThreadStyle threadStyle, ThreadEntryTPP threadEntry, void *threadParam, Size stackSize, ThreadOptions options, void **threadResult, ThreadID *threadMade );
The type of thread to create. Cooperative is the only type that you can specify. Historically, the Thread Manger supported two types of threads, preemptive and cooperative, but the Thread Manager no longer supports preemptive threads.
A pointer to the thread entry function.
A pointer to a value that the Thread Manager passes as a parameter to the thread entry function. Specify NULL if you are passing no information.
The stack size (in bytes) to allocate for this thread. This stack must be large enough to handle saved thread context, normal application stack usage, interrupt handling functions, and CPU exceptions. Specify a stack size of 0 (zero) to request the Thread Manager’s default stack size.
Options that define characteristics of the new thread. See the Thread Option Constants data type for details on the options. You sum the options together to create a single options parameter.
On return, a pointer to the address of a location to hold the function result provided by the Thread Option Constants function when the thread terminates. Specify NULL for this parameter if you are not interested in the function result.
On return, a pointer to the thread ID of the newly created or allocated thread. If there is an error, threadMade points to a value of kNoThreadID.
A result code. See “Thread Manager Result Codes.”
The NewThread function obtains a thread ID that you can use in other Thread Manager functions to identify the thread. If you want to allocate a thread from the pool of threads, specify the kUsePremadeThread option of the options parameter. Otherwise, NewThread creates a new thread.
When you request a thread from the existing pool, the Thread Manager allocates one that best fits your specified stack size. If you specify the kExactMatchThread option of the options parameter, the Thread Manager allocates a thread whose stack exactly matches your stack-size requirement or, if it can’t allocate one because no such thread exists, it returns the threadTooManyReqsErr result code.
Before making any calls to NewThread, be certain that you first have called the Memory Manager function MaxApplZone to extend the application heap to its limit. You must call MaxApplZone from the main application thread before any other threads in your application run.
When you call the NewThread function, you pass, as the threadEntry parameter, a pointer to the name of the entry function to the thread. When the newly created thread runs initially, it begins by executing this function.
You can use the threadParam parameter to pass thread-specific information to a newly created or allocated thread. In the data structure pointed to by this parameter, you could place something like A5 information or the address of a window to update. You could also use this parameter to specify a place for a thread’s local storage.
Be sure to create the storage for the threadResult parameter in a place that is guaranteed to be available when the thread terminates—for example, in an application global variable or in a local variable of the application’s main function (the main thread, by definition, cannot be disposed of so it is always available). Do not create the storage in a local variable of a subfunction that completes before the thread terminates or the storage will become invalid.
For Carbon applications, the pointer to your thread entry function must be a universal procedure pointer (UPP).
To dispose of a thread, use the DisposeThread function.
See the description of the Thread Option Constants data type for details on the characteristics you can specify in the options parameter.
For more information about the thread entry function, see the ThreadEntryProcPtr function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
For Carbon applications, you must create and pass a universal procedure pointer (UPP) to specify the new thread callback. Use the NewThreadEntryUPP and DisposeThreadEntryUPP functions to create and remove the UPP.
Threads.h
ThreadEntryUPP NewThreadEntryUPP ( ThreadEntryProcPtr userRoutine );
See the description of the ThreadEntryUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
ThreadSchedulerUPP NewThreadSchedulerUPP ( ThreadSchedulerProcPtr userRoutine );
See the description of the ThreadSchedulerUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
ThreadSwitchUPP NewThreadSwitchUPP ( ThreadSwitchProcPtr userRoutine );
See the description of the ThreadSwitchUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
ThreadTerminationUPP NewThreadTerminationUPP ( ThreadTerminationProcPtr userRoutine );
See the description of the ThreadTerminationUPP data type.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Installs functions that notify the debugger when a thread is created, disposed of, or scheduled.
Modified
OSErr SetDebuggerNotificationProcs ( DebuggerNewThreadTPP notifyNewThread, DebuggerDisposeThreadTPP notifyDisposeThread, DebuggerThreadSchedulerTPP notifyThreadScheduler );
A pointer to the callback function that notifies the debugger when a thread is created.
A pointer to the callback function that notifies the debugger when a thread is disposed of. This function is called whether you manually dispose of a thread with the DisposeThread function or if a thread disposes of itself automatically when it returns from its highest level of code.
A pointer to the callback function that notifies the debugger when a thread is scheduled.
A result code. See “Thread Manager Result Codes.”
You generally use this function only during development of an application.
The SetDebuggerNotificationProcs function installs three separate callback functions that return the thread ID of a newly created thread, the thread ID of a newly disposed of thread, and the thread ID of a newly scheduled thread.
The SetDebuggerNotificationProcs function always installs all three of the debugging functions. You cannot set only one or two of these functions, nor can you chain them together. These restrictions ensure that the function that calls SetDebuggerNotificationProcs owns all three of the debugging functions. If you want to prevent one or two of these debugging functions from being called, you can do so by setting them to NULL.
To guarantee that the debugger is getting an accurate view of scheduling, the Thread Manager doesn’t call the scheduling-notification callback function until both the generic Thread Manager scheduler and any custom thread scheduler have decided on a thread to schedule.
For Carbon applications, the pointers you pass to specify the callbacks must be universal procedure pointers (UPPs).
To create or allocate a new thread, use the NewThread function.
To dispose of a thread, use the DisposeThread function.
To schedule a thread, you can use a yield function such as YieldToAnyThread or YieldToThread or a function to change the state of a thread, such as SetThreadState.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
For Carbon applications, you must create and pass a universal procedure pointer (UPP) to specify the notification callbacks. You must use the designated UPP creation and disposal functions. For example, for the new thread notifier, you call the NewDebuggerNewThreadUPP and DisposeDebuggerNewThreadUPP functions.
Threads.h
Changes the state of a thread from stopped to ready when your application is not the current process.
OSErr SetThreadReadyGivenTaskRef ( ThreadTaskRef threadTRef, ThreadID threadToSet );
The thread task reference of the application containing the thread whose state you want to change.
The thread ID of the thread whose state you want to change.
A result code. See “Thread Manager Result Codes.”
When you mark a thread as ready to run with this function, the Thread Manager does not put it immediately into the scheduling queue but does so the next time it reschedules threads.
You can use SetThreadStateGivenTaskRef at times when you aren’t guaranteed that your application is the current context, such as during execution of an interrupt function. In such cases you must identify the thread task reference (the application context) as well as the thread ID.
You obtain the thread task reference for your application with the GetThreadCurrentTaskRef function.
The SetThreadReadyGivenTaskRef function allows you to do one thing only—change a thread from stopped to ready to execute. You cannot change the state of an executing thread to ready or stopped, nor can you change the state of a ready thread to executing or stopped with this call.
To determine the state of a thread when your application is not the current process, use the GetThreadStateGivenTaskRef function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Installs a custom scheduling function (custom scheduler).
Modified
OSErr SetThreadScheduler ( ThreadSchedulerTPP threadScheduler );
A pointer to a custom scheduler. Specify NULL if you want to remove an installed custom scheduler and use the default Thread Manager scheduling mechanism.
A result code. See “Thread Manager Result Codes.”
The SetThreadScheduler function installs a custom scheduler that runs in conjunction with the default Thread Manager scheduling mechanism. The Thread Manager uses a scheduler information structure to pass the custom scheduler the ID of the current thread and the ID of the thread that the Thread Manager has scheduled to run next.
A custom scheduler should return to the Thread Manager the ID of the thread that it determines to schedule. If it does not determine a particular thread to schedule, it should return the constant kNoThreadID and the Thread Manager default scheduling mechanism schedules the next thread.
If you already have a custom scheduler installed when you call SetThreadScheduler, it replaces the old one with a new one. If you want to remove your custom scheduler and return to using the default Thread Manager scheduling mechanism, call SetThreadScheduler and specify a value of NULL for the parameter.
The SetThreadScheduler function automatically disables scheduling to avoid any reentrancy problems with the custom scheduling function. Therefore, in your custom scheduling function, you should make no yield calls or other calls that would cause scheduling to occur.
For Carbon applications, the pointer to your thread scheduler function must be a universal procedure pointer (UPP).
For more information on the custom scheduling function, see the ThreadSchedulerProcPtr function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
For Carbon applications, you must create and pass a universal procedure pointer (UPP) to specify the thread scheduler callback. Use the NewThreadSchedulerUPP and DisposeThreadSchedulerUPP functions to create and remove the UPP.
Threads.h
Changes the state of any thread.
OSErr SetThreadState ( ThreadID threadToSet, ThreadState newState, ThreadID suggestedThread );
The thread ID of the thread whose state is to be changed.
The new state for the thread. You can specify ready to execute (kReadyThreadState), stopped (kStoppedThreadState), or executing (kRunningThreadState).
The thread ID of the next thread to run. You specify this thread if you are changing the state of the currently executing thread to stopped or ready to run. Pass kNoThreadID if you do not want to specify a particular thread to run next. In this case, the Thread Manager schedules the next available thread to run.
A result code. See “Thread Manager Result Codes.”
The effect of SetThreadState depends on whether the thread you specify for changing is the currently executing thread or another thread. If you specify the current thread and thus change the state to stopped or ready, SetThreadState invokes the Thread Manager scheduling mechanism. The current thread relinquishes control (it is put in the state you specify, stopped or ready) and the Thread Manager schedules the thread that you specify with the suggestedThread parameter. If this thread is unavailable for running, or if you passed kNoThreadID, the Thread Manager schedules the next available thread.
If you change the state of the current thread to ready, the Thread Manager suspends it awaiting of the CPU. When it is rescheduled, SetThreadState regains control and returns to the function that called it.
If you have installed a custom scheduler, the Thread Manager passes it the thread ID of the suspended thread.
If you specify a thread other than the currently executing thread, no rescheduling occurs. If you change the state from ready to stopped, the thread is removed from the scheduling queue. The Thread Manager does not schedule this thread for execution again until you change its state to ready. On the other hand, if you change the state from stopped to ready, you have in effect put the thread in the scheduling queue, and the Thread Manager gives it CPU time as soon as it reaches the top of the scheduling queue.
Threads must yield in the CPU addressing mode (24-bit or 32-bit) in which the application was launched.
To obtain the state of any thread, use the GetThreadState function.
To relinquish control to the next available thread, use the YieldToAnyThread function. To relinquish control to a specific thread, use the YieldToThread function.
To set the state of the current thread before it exits a critical section of code, use the SetThreadStateEndCritical function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Changes the state of the current thread and exits that thread’s critical section at the same time.
OSErr SetThreadStateEndCritical ( ThreadID threadToSet, ThreadState newState, ThreadID suggestedThread );
The thread ID of the thread whose state is to be changed.
The new state for the thread. You can specify ready to execute (kReadyThreadState), stopped (kStoppedThreadState) or executing (kRunningThreadState).
The thread ID of the next thread to run. You specify this thread if you are changing the state of the currently executing thread to stopped or ready to run. Pass kNoThreadID if you do not want to specify a particular thread to run next. In this case, the Thread Manager schedules the next available thread to run.
A result code. See “Thread Manager Result Codes.”
The SetThreadStateEndCritical function does in one step the same thing that ThreadEndCritical and SetThreadState functions do in two steps.
Historically, the primary purpose of the SetThreadStateEndCritical function was to close the scheduling window at the end of a critical section. A preemptive thread that was waiting while the critical section of code was executing could begin executing before you changed the state of the current thread to stopped with the SetThreadState function. Obviously, because the Thread Manager no longer supports preemptive threads, this function is no longer necessary to close the scheduling window, but you can still use it to change the state of a thread and exit a critical section in one step instead of two.
When you change the state of the currently executing thread, the Thread Manager schedules the thread you specify with the suggestedThread parameter. If this thread is unavailable or if you pass kNoThreadID, the Thread Manager schedules the next available thread.
To mark a section of code as critical, use the ThreadBeginCritical and the ThreadEndCritical functions.
To change the state of any thread, use the SetThreadState function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Installs a custom context-switching function for any thread.
Modified
OSErr SetThreadSwitcher ( ThreadID thread, ThreadSwitchTPP threadSwitcher, void *switchProcParam, Boolean inOrOut );
The thread ID of the thread to associate with a context-switching function.
A pointer to the context-switching function.
A pointer to a thread-specific parameter that you pass to the context-switching function.
A Boolean value that indicates whether the Thread Manager calls the context-switching function when the specified thread switches in (True) or when it is switched out by another thread (False).
A result code. See “Thread Manager Result Codes.”
The custom switching function allows you to save context information in addition to the default context information that the Thread Manager automatically saves when it switches contexts. The default context information consists of the CPU registers, the FPU registers (if any), and the location of the thread’s context.
You must actually define two context-switching functions, one for leaving a thread and another for entering a thread. When leaving a thread, you call the outer context-switching function to save additional context information. When reentering a thread, you call the inner context-switching function to restore the extra information that was saved on exit. Use the inOrOut parameter of the SetThreadSwitcher function to specify which type of context-switching function is being installed.
You can pass a different switchProcParam parameter to each thread, which allows you to write a single, application-wide custom switching function and then pass any thread-specific information when the Thread Manager calls the switching function for that thread.
The SetThreadSwitcher function automatically disables scheduling to avoid any reentrancy problems with the custom switching function. Therefore, in the custom switching function, you should make no yield calls or other calls that would cause scheduling to occur.
For Carbon applications, the pointer to your thread switcher function must be a universal procedure pointer (UPP).
For more information on the custom context-switching function, see the ThreadSwitchProcPtr function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
For Carbon applications, you must create and pass a universal procedure pointer (UPP) to specify the thread switcher callback. Use the NewThreadSwitchUPP and DisposeThreadSwitchUPP functions to create and remove the UPP.
Threads.h
Installs a custom thread-termination function for a thread.
Modified
OSErr SetThreadTerminator ( ThreadID thread, ThreadTerminationTPP threadTerminator, void *terminationProcParam );
The thread ID of the thread to associate with the thread-termination function.
A pointer to the thread-termination function.
A pointer to a thread-specific parameter that you pass to the thread-termination function.
A result code. See “Thread Manager Result Codes.”
The Thread Manager calls the custom termination function whenever the specified thread completes execution of its code or when you manually dispose of the thread with the DisposeThread function.
You can pass a different terminationProcParam parameter to each thread, which allows you to write a single, application-wide custom thread-termination function and then pass any thread-specific information when the Thread Manager calls the termination function for that thread.
For Carbon applications, the pointer to your thread terminator function must be a universal procedure pointer (UPP).
For more information on the custom thread-termination function, see the ThreadTerminationProcPtr function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
For Carbon applications, you must create and pass a universal procedure pointer (UPP) to specify the thread terminator callback. Use the NewThreadTerminationUPP and DisposeThreadTerminationUPP functions to create and remove the UPP.
Threads.h
Indicates that the thread is entering a critical code section.
OSErr ThreadBeginCritical ( void );
A result code. See “Thread Manager Result Codes.”
The ThreadBeginCritical function disables scheduling by marking the beginning of a section of critical code. That is, no other threads in the current application can run—even if the current thread yields control—until the current thread exits the critical section (by calling the ThreadEndCritical function). Disabling scheduling allows the currently executing function to look at or change shared or global data safely. You can nest critical sections within a thread.
To mark the end of a critical code section and turn scheduling back on, use the ThreadEndCritical function. If you also need to set the state of the current thread before scheduling is turned back on, use the SetThreadStateEndCritical function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Determines the amount of stack space that is available for any thread in your application.
OSErr ThreadCurrentStackSpace ( ThreadID thread, ByteCount *freeStack );
The thread ID of the thread about which you want information.
On return, a pointer to the amount of stack space (in bytes) that is available to the specified thread.
A result code. See “Thread Manager Result Codes.”
This function is primarily useful during debugging since you determine the maximum amount of stack space you need for any particular thread before you ship your application. However, if your application calls a recursive function that could call itself many times, you might want to use ThreadCurrentStackSpace to keep track of the stack space and take appropriate action if it becomes too low.
To determine the default size that the Thread Manager assigns to threads use the GetDefaultThreadStackSize function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Indicates that the thread is leaving a critical code section.
OSErr ThreadEndCritical ( void );
A result code. See “Thread Manager Result Codes.”
After a call to the Thread, all scheduling operations are now available to the application.
Use the ThreadBeginCritical function to mark the beginning of a critical code section and turn scheduling off.
If you need to set the state of the current thread before scheduling is turned back on, use the SetThreadStateEndCritical function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Relinquishes the current thread’s control.
OSErr YieldToAnyThread ( void );
A result code. See “Thread Manager Result Codes.”
The YieldToAnyThread function invokes the Thread Manager’s scheduling mechanism. The current thread relinquishes control and the Thread Manager schedules the next available thread.
The current thread is suspended in the ready state and awaits rescheduling when the CPU is available. When the suspended thread is scheduled again, YieldToAnyThread regains control and returns to the function that called it.
If you have installed a custom scheduler, the Thread Manager passes it the thread ID of the suspended thread.
In each thread you must make one or more strategically placed calls to relinquish control to another thread. You can either make this yield call or another yield call such as YieldToThread; or you can make a call such as SetThreadState to explicitly change the state of the thread.
Threads must yield in the CPU addressing mode (24-bit or 32-bit) in which the application was launched.
To relinquish control to a specific thread, use the YieldToThread function.
To change the state of a specified thread, use the SetThreadState function.
Active development with the Thread Manager is not recommended. The API is intended only for developers who are porting their applications to Mac OS X and whose code relies on the cooperative threading model. If you are writing a new Carbon application, you should use POSIX threads or the Multiprocessing Services API instead. See Threading Programming Guide for more information.
Threads.h
Relinquishes the current thread’s control to a particular thread.
OSErr YieldToThread ( ThreadID suggestedThread );
The ID of the thread to yield control to.
A result code. See “Thread Manager Result Codes.”
The YieldToThread function invokes the Thread Manager’s scheduling mechanism. The current thread relinquishes control and passes the thread ID of a thread for the Thread Manager to schedule. The Thread Manager schedules this thread if it is available. Otherwise, the Thread Manager schedules the next available thread.
The current thread is suspended in the ready state and awaits rescheduling when the CPU is available. When the suspended thread is scheduled again, YieldToThread regains control and returns to the function that called it.
If you have installed a custom scheduler, the Thread Manager passes it the thread ID of the suspended thread.
In each thread you must make one or more strategically placed calls to relinquish control to another thread. You can either make this yield call or another yield call such as YieldToAnyThread; or you can make a call such as SetThreadState to e