News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

thread TerminateThread and SECURITY_DESCRIPTOR

Started by TouEnMasm, September 02, 2014, 05:38:17 PM

Previous topic - Next topic

jj2007

Quote from: Zen on September 03, 2014, 05:08:56 AM
Jeffrey Richter and Christophe Nasarre ... conclude that section with this statement:
"To create a new thread, you must not call the operating system's CreateThread function—you must call the C/C++ run-time library function _beginthreadex."

For Masm32, this is irrelevant. Microsoft support:
QuoteThreads that are created and terminated with the CreateThread() and ExitThread() Win32 API functions do not have memory that is allocated by the CRT for static data and static buffers cleaned up when the thread terminates. Some examples of this type of memory are static data for errno and _doserrno and the static buffers used by functions such as asctime(), ctime(), localtime(), gmtime(), and mktime(). Using CreateThread() in a program that uses the CRT (for example, links with LIBCMT.LIB) may cause a memory leak of about 70-80 bytes each time a thread is terminated.

So, yes, you can have trouble if you create a Million threads that call asctime(), but otherwise CreateThread is safe. If it wasn't, a lot of professional software would crash.

Zen

JOCHEN,
This is great Intel,...thanks,...
...But,...just to be TOTALLY ANNOYING,...isn't it possible to call a C/C++ runtime function in a MASM Assembly Program ???
Zen

adeyblue

Yep, but it only leaks if your program statically links to the CRT, as after allocating the per-thread data structure, the static CRT has no way to be notified of when the thread ends. They recommend using _beginthreadex because that wraps the thread function you give in order to run the cleanup code when it returns. Calling functions in msvcrt is fine as it gets the normal DLL_THREAD_DETACH notification.

Also:
Quote
However, if TerminateThread is used, the system does not destroy the thread's stack until the process that owned the thread terminates.
They changed this for Vista. TerminateThread frees the stack now. Not that that's any justification for using it though.

jj2007

Quote from: adeyblue on September 03, 2014, 10:20:51 AM
They changed this for Vista. TerminateThread frees the stack now. Not that that's any justification for using it though.

Cute. The justification for the old behaviour was that other threads could continue using that stack instead of crashing. Which implies they are crashing now ::)

btw completely irrelevant, as nobody should ever be forced to use TerminateThread, it's just utterly bad design 8)