Another thing: if you use the C runtime at all, you should use __beginthreadex instead of CreateThread to start the new thread. It's defined in <process.h>.
Another thing: if you use the C runtime at all, you should use __beginthreadex instead of CreateThread to start the new thread. It's defined in <process.h>.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Why? CreateThread is a Win32 API call, so that's what you are meant to use in Windows.Another thing: if you use the C runtime at all, you should use __beginthreadex instead of CreateThread to start the new thread. It's defined in <process.h>.
"The Internet treats censorship as damage and routes around it." - John Gilmore
Because _beginthreadex does some extra initializing before calling CreateThread in turn, and also some cleanup before the thread finishes. If you use CreateThread directly, and then any CRT function that uses thread-local resources (quite a few of them), you leak resources for every thread that dies.
(I got the name wrong, though: it's only a single underscore in front.)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
It's documented in msdn and I'm fairly sure it's also mentioned in either (or both) Petzold's 'Windows Programming' or Richter's 'Programming Applications for MS Windows'.Originally Posted by msdn,CreateThread
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Petzold uses _beginthread, which is fine for some uses, but provides fewer options. It is also paired with _endthread (which a thread created with _beginthread calls automatically upon ending!), which does not allow you to retrieve the thread exit code, because it closes the thread handle and, because _beginthread does not permit the creation of a sleeping thread, prevents the existence of a reliable way of duplicating it first. It's Richter who points out the problems and strongly recommends _beginthreadex over _beginthread.
Richter also has this to say about using CreateThread instead of _beginthreadex (paraphrased, translating back to English from my German version):
Of course, a few problems remain. For example, the entire process is terminated if the thread calls the C/C++ runtime function signal, because the environment is not prepared for structured exception handling. In addition, if the thread ends without a call to _endthreadex, the data block [for the CRT thread-local data] cannot be freed, resulting in a memory leak. (And who would call _endthreadex for a thread created with CreateThread?)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
If CreateThread threads would never free data, it would cause many many problems. I haven't seen these problems and that's why I don't believe this part.the data block [for the CRT thread-local data] cannot be freed
"The Internet treats censorship as damage and routes around it." - John Gilmore