Thread: multithreading in C++

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    abachler, you claim the wrong problem and then accuse those who know better of being sheep.

    CreateThread isn't broken. It does what it does perfectly well: it creates a new Win32 thread.

    The problem isn't that it's broken, it's that it's unaware. The MS CRT needs some special handling at thread start and end, else it will leak memory. CreateThread is a pure Win32 function and does not - must not, even - handle this CRT stuff. That's why the MS CRT provides its own wrapper around CreateThread: _beginthreadex.



    I have used Boost.Threads every now and then and can only say good things about it. I haven't used it since the last big interface change, though.
    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
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    So show me some code where the CRT will leak memory because of the use of CreateThread.

    Quote Originally Posted by MSDN
    If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
    which is generally a non-issue unless your code specifically follows an 'allocate until refused' paradigm, but it says nothig about leaking memory.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    char *tok = strtok(input, seps);

    The CRT DLL will still reclaim the memory; the static CRT will not.
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't understand why you are refusing to acknowledge that CreateThread has problems. _beginthreadex will still give you the same result as CreateThread but without the problems that come with it. What is the point of arguing here other than just to argue? I would think you would care about the end product more than about being right. Perhaps I'm wrong.

  5. #20
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    straight from the source (and 1st result for googling 'createthread memory leak')

    http://support.microsoft.com/kb/104641

    All C Run-time functions except the signal() function work correctly when used in threads that are created by the CreateThread() function. However, depending on what CRT functions are called, there may be a small memory leak when threads are terminated. Calling strlen(), for example, does not trigger the allocation of the CRT thread data-block, and calling malloc(), fopen(), _open(), strtok(), ctime(), or localtime() causes allocation of a CRT per-thread data-block, which may cause a memory leak.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Question on Multithreading
    By ronan_40060 in forum C Programming
    Replies: 1
    Last Post: 08-23-2006, 07:58 AM
  3. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM
  4. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 09:28 PM
  5. Multithreading
    By -KEN- in forum C# Programming
    Replies: 4
    Last Post: 06-13-2003, 10:11 PM