INF: How Thread Stack Memory Is Handled When Threads Terminate

ID Number: Q66295

6.00 6.00a

OS/2

Summary:

In Microsoft C versions 6.0 and 6.0a, if _beginthread() is called with

NULL for the thread stack, malloc() will be called to allocate a stack

from the heap of size stack_size (passed as the third parameter). At

the time the thread is terminated, the stack is still in use;

therefore, _endthread() does not automatically free it. In fact, the

last thing _endthread() does is push the appropriate arguments onto

the stack and call DosExit().

However, the memory is recovered. The next time that thread ID is

used, _beginthread() will check to see if the thread stack was

previously malloc()'ed internally by the function. If so, it will call

free() at that time. Note that the thread memory does not go to the

operating system; instead, it is returned to the heap. Because OS/2

will use a thread ID from terminated threads first, there will usually

be only one thread stack not freed with free().

Additional reference words: 6.00 6.00a