Compile Errors for CreateThread..

This is a discussion on Compile Errors for CreateThread.. within the Windows Programming forums, part of the Platform Specific Boards category; Another thing: if you use the C runtime at all, you should use __beginthreadex instead of CreateThread to start the ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  2. #17
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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>.
    Why? CreateThread is a Win32 API call, so that's what you are meant to use in Windows.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  4. #19
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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'.
    Quote Originally Posted by msdn,CreateThread
    A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  6. #21
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    the data block [for the CRT thread-local data] cannot be freed
    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 Internet treats censorship as damage and routes around it." - John Gilmore

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange compile errors
    By csonx_p in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2008, 11:41 AM
  2. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 05:00 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21