You are currently offline, waiting for your internet to reconnect

Your browser is out-of-date

You need to update your browser to use the site.

Update to the latest version of Internet Explorer

Description of using C Run-Time (CRT) functions and CreateThread ()

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Summary
All C Run-time functions except the signal() function work correctlywhen used in threads that are created by the CreateThread() function. However,depending on what CRT functions are called, there may be a small memoryleak when threads are terminated. Calling strlen(), for example, does nottrigger the allocation of the CRT thread data-block, and calling malloc(),fopen(), _open(), strtok(), ctime(), or localtime() causes allocation of aCRT per-thread data-block, which may cause a memory leak.
More information
The "Programming Techniques" manual supplied with Visual C++ 32-bit Editionstates that using CreateThread() in a program that uses Libcmt.lib causesmany CRT functions to fail. Actually, the only function that should not beused in a thread created with CreateThread() is the signal() function.

There are two ways to create threads. One method involves using the CRT_beginthread() or _beginthreadex() (with Visual C++ 2.0 and later); theother method involves using the CreateThread() API. All CRT functions otherthan the signal() function work correctly in threads created with either_beginthread() or CreateThread(). However, there are some problems involvedwith using CRT functions in threads created with CreateThread().

Threads that are created and terminated with the CreateThread() andExitThread() Win32 API functions do not have memory that is allocated bythe CRT for static data and static buffers cleaned up when the threadterminates. Some examples of this type of memory are static data for errnoand _doserrno and the static buffers used by functions such as asctime(),ctime(), localtime(), gmtime(), and mktime(). Using CreateThread() in aprogram that uses the CRT (for example, links with LIBCMT.LIB) may cause amemory leak of about 70-80 bytes each time a thread is terminated.

To guarantee that all static data and static buffers allocated by theCRT are cleaned up when the thread terminates, _beginthreadex() and_endthreadex() should be used when creating a thread. The _beginthreadex()function includes the same parameters and functionality as CreateThread().

Note It is not possible to terminate a thread with _endthreadex() when itwas created with CreateThread().
Properties

Article ID: 104641 - Last Review: 06/17/2014 21:46:00 - Revision: 4.0

  • kbhowto kbinfo KB104641
Feedback
>://c1.microsoft.com/c.gif?DI=4050&did=1&t="> var varCustomerTracking = 1; var Route = "76500"; var Ctrl = ""; document.write(" ')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> did=1&t=">