1

When I write C/C++ code for Windows platform, I usually use Windows APIs as necessary. But when it comes to multi-threading, I read the following quotaion from < Windows via C/C++ >

The CreateThread function is the Windows function that creates a thread. However, if you are writing C/C++ code, you should never call CreateThread. Instead, you should use the Microsoft C++ run-time library function _beginthreadex. If you do not use Microsoft's C++ compiler, your compiler vendor will have its own alternative to CrateThread. Whatever this alternative is, you must use it.

AFAIK, a language run-time library for a certain platform is implemented with that platform's APIs. I think it is totally possible to call CreateThread() from my C/C++ code. And I quite did that. But I just don't understand why the above rule should be followed.

Many thanks for your insights.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • Possible duplicate of http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c – Moo-Juice Nov 16 '10 at 07:42

2 Answers2

4

Of course it's possible to use the Windows API's CreateThread directly.

But that leaves the runtime library uninformed about the new thread.

For the multi-threading support in the runtime library (and that includes functions that rely on static storage, e.g. I imagine it includes strtok) you need to keep the runtime library informed, and not only informed, but partially in charge so that e.g. failure to allocate whatever resources it needs for a thread, results in thread creation failure.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
3

The c-runtime has numerous stateful variables that hold things, such as the current locale. These values have to be settable per thread otherwise code in oen thread (e.g. calling setlocale) could unduly influence code running in a different thread.

_beginthread wraps your thread in code that performs the necessary allocation AND deallocation of these per thread data structures.

If you call CreateThread directly, the structures will probably be allocated as required, but without the wrapper, the runtime will never know when the thread exits and they will leak.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • After I read about < Windows via C/C++ 5th Edition > Chapter 6 Secion: C/C++ Run-Time Library, I found the per thread stateful variables you mentioned. I agree that these variables are necessary. But I'm wondering why they are not implemented by the Windows, but implemented by the language run time library? – smwikipedia Nov 22 '10 at 03:27
  • So Windows does provide quite a lot of infrastructure for threading and other things, **while leaving enough room for language-specific things**. And the stateful variables you mentioned are the very example of these language-specific things. Am I right? – smwikipedia Nov 22 '10 at 03:29
  • Those variables are language specific yes. Other languages (or even different versions of the c-runtime) could implement similar functionality a completely different way. – Chris Becke Nov 22 '10 at 08:49