Thread: multithreading in C++

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    multithreading in C++

    how to do multithreading in C++ on Windows platform....

    What would be good??? do it using the Win32 platform or Boost library ??? or is there any other waY?


    EDIT: I haven't done any multithreading programming and have no idea of it.. So what should I learn????
    Last edited by manzoor; 11-27-2008 at 07:36 AM.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    CreateThread() is my favorite, of course the flamewar will now begin (again) between me and people that prefer beginthread() or boost. Theres also the OpenMP option, depending on what specifically you are trying to accomplish.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...Basically, it says use beginthread, unless you are using MFC. It's the safest way and it will avoid all the issues vs the discussion.
    I don't know if Boost is better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    isn't beginthread() a C function ?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, it is. There is no C++ equivalent.
    CreateThread is a C function, too.
    But you can try Boost's threads too. They are C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    msdn says to use beginthread if you're using the C runtime library, or you can leak stuff. (so i guess CreateThread if you're only linking C++ libs)
    AfxBeginThread for MFC.

    also, boost's implementation uses _beginthreadex

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Never use _beginthread. It's conceptually broken. It's been completely superseded by _beginthreadex.

    Never use raw CreateThread if you use the C or C++ runtime library. abachler may call it a flamewar of people with different preferences, but it's really about one thing being wrong and the other correct, with no space for opinion.

    Boost's threading library is superior to all C-style alternatives, though, because it provides RAII synchronization primitives and allows you to provide an arbitrary function object (including a bound member function) as the thread starting function.
    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

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I always use _beginthreadex b/c MSDN specifically says do not use CreateThread or it mentions that one should prefer to use _beginthread or _beginthreadex. _beginthreadex is the best choice for multi-threading...at least for now.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What exactly is it about CreateThread() that's broken, and can't they fix whatever is broken?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Its not broken, but you can leak handles if you dont track them and close them when the thread terminates. Theres also some funky stuff that can happen if you creat the thread as suspended without enough memory to allocate its stack, but that isnt limited to CreateThread. Theres a fairly lengthy thread somewhere where we realyl got into it and I explained the issues and non-issues with CreateThread vs beginthread(ex). The fact is I have never had a problem with CreateThread, and it isnt from a lack of trying.

    Just because others have fallen to their deaths doesnt mean I am wrong for working without a net.
    Last edited by abachler; 11-27-2008 at 10:15 PM.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think I use it too (not that I create multi-threaded apps that often), although I never use MFC.
    But why doesn't beginthreadex() have that same problem then?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    It does, but some people read something on MSDN and take it as gospel, because Microsoft is infallable dont you know.

    I challenge anyone to show concrete proof (i.e. actual code) showing CreateThread being broken where beginthread works. Im tired of all this ad hominem appeal to authority crap.
    Last edited by abachler; 11-27-2008 at 10:28 PM.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Im tired of all this ad hominem appeal to authority crap.
    You seem to be tired of any type of authority.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I would say that Microsoft is the ultimate authority on their own software. Whether you believe that or not, I don't care, but I think it is a pretty safe assumption. If Microsoft recommends that a particular function be used instead of another, I see no reason to doubt it.

    Personally, I prefer pthreads. it is multi-platform, and works very well for anything I've ever needed it for.

  15. #15
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I have read with interest about Boost's solution. Does anybody have any first-hand experience with the library?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

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