dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: _beginthread vs CreateThread

  1. #1
    Join Date
    Sep 2002
    Posts
    115

    Question _beginthread vs CreateThread

    I have a C project that uses some threads.

    I currently don't use ExitThread(), instead my threads simply return (x) when they are done. I plan to use ExitThread() instead though, as it generally seems like a "safer" method.

    I create the threads with CreateThread().
    Question:
    If I change from simply using return in my threads to use ExitThread(), should I then also change from using CreateThread() to _beginthread() to create my threads?

    I ask this because MSDN states:
    " A thread that uses functions from the C run-time libraries should use the
    _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.
    "

    Is there perhaps more to this!?
    Please enlighten me!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: _beginthread vs CreateThread

    You should not mix those thread functions. Either use _beginthread/_endthread, _beginthreadex/_endthreadex or CreateThread/ExitThread.

    If you dont use ExitThread to terminate a thread the API will implicit call ExitThread for you.

    Is there perhaps more to this!?
    The C runtime (or some of its functions) uses the Thread Local Storage (TLS) to store information. If you create a thread using CreateThread then this information will not be initialized, and the C runtime may and will misbehave.

    - petter

  3. #3
    Join Date
    Dec 2005
    Posts
    642

    Re: _beginthread vs CreateThread

    Using "return (x)" is the best way, since it's compatible with _beginthread, _beginthreadex and CreateThread. Whoever the caller is you're returning to will issue a matching _endthread, _endthreadex or EndThread, according to the situation.

  4. #4
    Join Date
    Sep 2002
    Posts
    115

    Re: _beginthread vs CreateThread

    Quote Originally Posted by wildfrog
    You should not mix those thread functions. Either use _beginthread/_endthread, _beginthreadex/_endthreadex or CreateThread/ExitThread.

    If you dont use ExitThread to terminate a thread the API will implicit call ExitThread for you.

    The C runtime (or some of its functions) uses the Thread Local Storage (TLS) to store information. If you create a thread using CreateThread then this information will not be initialized, and the C runtime may and will misbehave.

    - petter
    Thanks!

    OK, so if I sum this up, this is the facts:

    1. When programming in C, I should use _beginthread() to create my threads (never CreateThread()).

    2. I can safetly continue using return(x) when exiting in my threads (no point in explicitly calling _endthread and never call ExitThread()).

    Correct?

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: _beginthread vs CreateThread

    1. When programming in C, I should use _beginthread() to create my threads (never CreateThread()).
    I wouldn't say never, but as a generals rule use _beginthread or _beginthreadex.

    2. I can safetly continue using return(x) when exiting in my threads (no point in explicitly calling _endthread and never call ExitThread()).
    Correct.

    - petter

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: _beginthread vs CreateThread

    Quote Originally Posted by ackabacka
    OK, so if I sum this up, this is the facts:

    1. When programming in C, I should use _beginthread() to create my threads (never CreateThread()).

    2. I can safetly continue using return(x) when exiting in my threads (no point in explicitly calling _endthread and never call ExitThread()).

    Correct?
    Yes...and

    3. Prefer '_beginthreadex()' over the other ones to avoid running into problems with the C runtime...

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,986

    Re: _beginthread vs CreateThread

    Quote Originally Posted by Andreas Masur
    3. Prefer '_beginthreadex()' over the other ones to avoid running into problems with the C runtime...
    Andeas, I remember reading about the benefits in Ricter's Advanced Windows a while back, but don't recall the details. Do you recall why _beginthreadex is preferable over _beginthread and CreateThread?

    Arjay

  8. #8
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: _beginthread vs CreateThread

    Do you recall why _beginthreadex is preferable over _beginthread and CreateThread?
    That fact that it 'resembles the Win32 CreateThread API more closely than _beginthread does' is one reason.

    A couple of points are listed under the remarks section here.

    - petter

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,986

    Re: _beginthread vs CreateThread

    Thanks Wildfrog, I've been using _beginthreadex for so long now I didn't even think to look at the docs. Way back when, this info wasn't in the msdn documentation, I recalled it was only in Richter's book.

    RTFM really does work <g>

    Arjay

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: _beginthread vs CreateThread

    Quote Originally Posted by Arjay
    Andeas, I remember reading about the benefits in Ricter's Advanced Windows a while back, but don't recall the details. Do you recall why _beginthreadex is preferable over _beginthread and CreateThread?
    The reason behind this is simply that the C runtime library was first invented long before multithreading was available. Thus, it was designed as a single-threaded library. The problem is simply the usage of global variables and functions such as 'errno' and 'strtok()'. If you have a multithreaded application consisting of two threads that both call a runtime function and check 'errno' to verify the result, it might come to a deadlock:
    • The first thread calls the runtime function
    • Before it can check the global 'errno' variable, a context switch to the second thread occurs
    • The second thread also calls a runtime function
    • When the context switch occrus back to the first thread, the value of 'errno' rather reflects the result of the function call from the second thread

    Thus, in order to work in multi-threaded environments, the runtime library needs to maintain these global variables local to each thread. And that is exactly what '_beginthreadex()' does. These variables are allocated into a specific area named thread-local storage. It provides a unique memory area for each thread in the system, thus, binding the global variables to this thread. The thread then knows how to retrieve these data and the above given example would work correctly since both the first and the second thread would have a separate global variable 'errno'. 'CreateThread()' does skip this initialization and thus, can cause some hard to find bugs in your code...
    Last edited by Andreas Masur; January 9th, 2006 at 05:46 PM.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,986

    Re: _beginthread vs CreateThread

    Thanks Andreas.

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: _beginthread vs CreateThread

    Quote Originally Posted by Arjay
    Thanks Andreas.
    You are welcome...

  13. #13
    Join Date
    Sep 2002
    Posts
    115

    Red face Re: _beginthread vs CreateThread

    Quote Originally Posted by Arjay
    Thanks Andreas.
    Yeah, thanks!

    I've had some really wierd problems with one of the threads in my C project. Changing from CreateThread to _beginthreadex might wipe them out.

    I've just hunted down a copy of Richter's "Programming Applications for Microsoft Windows (Dv-Mps General)" (the follow up of Advanced Windows). It was darn hard to get and it will probably takes months before it's delivered but it's about time that I get it. ;-)

    Any other book on multi threading in Windows anyone could recommend?
    What about these?
    * Multi-Threaded Programming in C++ (Hardcover).
    * Multithreading Applications in Win32: The Complete Guide to Threads (Paperback).

  14. #14
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: _beginthread vs CreateThread

    Take a look at the following reviews of these books:

  15. #15
    Join Date
    Sep 2002
    Posts
    115

    Re: _beginthread vs CreateThread

    Quote Originally Posted by Andreas Masur
    The reason behind this is simply that the C runtime library was first invented long before multithreading was available. Thus, it was designed as a single-threaded library. The problem is simply the usage of global variables and functions such as 'errno' and 'strtok()'. If you have a multithreaded application consisting of two threads that both call a runtime function and check 'errno' to verify the result, it might come to a deadlock:
    • The first thread calls the runtime function
    • Before it can check the global 'errno' variable, a context switch to the second thread occurs
    • The second thread also calls a runtime function
    • When the context switch occrus back to the first thread, the value of 'errno' rather reflects the result of the function call from the second thread

    Thus, in order to work in multi-threaded environments, the runtime library needs to maintain these global variables local to each thread. And that is exactly what '_beginthreadex()' does. These variables are allocated into a specific area named thread-local storage. It provides a unique memory area for each thread in the system, thus, binding the global variables to this thread. The thread then knows how to retrieve these data and the above given example would work correctly since both the first and the second thread would have a separate global variable 'errno'. 'CreateThread()' does skip this initialization and thus, can cause some hard to find bugs in your code...

    With a chance of starting to become bothersome ;-), I simply must ask this.

    I found in a book extract on MSDN:

    "_beginthread performs some of the internal initialization for a new thread that other C runtime functions, such as signal, depend on. The rule is consistency: If your program manipulates threads with C runtime functions, then use only C runtime functions wherever you have a choice. If your program uses Win32 functions with its threads, then stick to CreateThread and ExitThread. Also, if the thread calls C runtime functions, then create it with the C functions rather than the Win32 API. A few C routines require the initialization that _beginthread performs."

    Does this mean that also beginthreadex is badly suited when using C and Win32 API calls??? Or is it only beginthread that is bad suited when using Win32 API calls?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)