# OS_SetThreadDestructor

## Syntax

      #include <nitro/os.h>

      void OS_SetThreadDestructor( OSThread* thread, OSThreadDestructor dtor );

## Arguments

thread | Pointer to the thread structure used to set the destructor.
dtor | Destructor function.

## Return Values

None.

## Description

This function sets the destructor function to be called when a thread ends.

The thread is specified by thread. dtor is a pointer to a function of type `OSThreadDestructor`. The `OSThreadDestructor` type is defined as:

`typedef void (*OSThreadDestructor)( void* );`

The destructor function is called when a thread ends under the following conditions:

> - When the thread finishes executing its task
> - When it ended with `OS_ExitThread()`
> - When it was terminated from another thread with `OS_KillThread*()`
> - When it terminated itself with `OS_KillThread*()`

A destructor function is not called in the following situations.

> ·- When the thread was terminated by another thread using `OS_DestroyThread()`
> - When the thread terminated itself using `OS_DestroyThread()`

When the destructor function is run, execution switches to the context of the thread that ends. The stack pointer is then updated (described later). The destructor is called while the thread is in interrupt-prohibited state, before internal thread information is changed.

At the time the destructor function is executed, the stack is shifted to highest priority by default (excluding the code used for checking). You can change it to the specified stack using `OS_SetThreadDestructorStack()`.

Do not release the prohibition on interrupts inside the destructor function or call any functions that generate thread scheduling, because this may cause mismatching to arise in the thread list.

## See Also

`OS_InitThread`, `OS_ExitThread`, `OS_DestroyThread`, `OS_KillThread*`, `OS_GetThreadDestructor`, `OS_SetThreadDestructorStack`

## Revision History

2007/08/20 Added notes about operations inside the destructor function.
2005/08/09 Added text about `OS_SetThreadDestructorStack`.
2005/07/07 Added text about conditions under which destructor function is called.
2005/06/30 Initial version.

CONFIDENTIAL