CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Console Multithreading

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Console Multithreading

    I'm relatively new to c++ but am trying to move along. I have Visual Studio 2010 and am using C++ and cannot find a easy to follow guide on how to run 2 threads at once without getting a boatload of code thats not relevant to understanding the threads... if someone could show me a simple bit of console program code that increments a value in one thread and decrements a different value in the other I would be grateful .

    (i'm 1 day out of surgery but I think all of this makes sense, let me know if it doesn't!)

    Thank you,
    Daniel Smith

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Console Multithreading

    C and C++ have no built in support for multithreading like Java or C#. If you want threading you need to use a 3rd party library like OpenMP or boost, or do things directly through the operating system.

    In Windows you would use the CreateThread function, found in windows.h.
    Last edited by Chris_F; April 17th, 2010 at 09:16 PM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Console Multithreading

    Actually, the next version of the standard (C++0x) will have threading support. I don't think that part of it is implemented on many compilers yet though.

    The most popular cross-platform options for now are pthreads (which uses a C-based API) and Boost Threads (C++ API).

    First, choose which threading library you want to use.

  4. #4
    Join Date
    Apr 2010
    Posts
    3

    Re: Console Multithreading

    Thank you very much! I was struggling trying to figure out the windows.h one but for some reason the OpenMP one just makes sense to me. I can't imagine why they have taken this long to make multi threading part of the standard though...

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: Console Multithreading

    Quote Originally Posted by Zamadatix View Post
    Thank you very much! I was struggling trying to figure out the windows.h one but for some reason the OpenMP one just makes sense to me. I can't imagine why they have taken this long to make multi threading part of the standard though...
    OpenMP isn't always the right method of multithreading. It's easy to use and implement on certain types of algorithms, but sometimes you'll need to use regular threads.

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

    Re: Console Multithreading

    Since you are new to C++, start by understanding the basics before dealing with libraries such as Boost or OpenMP which both will raise more questions than anything else.

    Use what comes with your compiler as a starting point, understand the fundamentals of threading, play around with simple applications and once you get a better understanding you can start looking at alternative (and more platform-independent) ways of threading.

    Don't start running before you can walk....look at the following for example:


  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Console Multithreading

    Quote Originally Posted by Andreas Masur View Post
    Since you are new to C++, start by understanding the basics before dealing with libraries such as Boost or OpenMP which both will raise more questions than anything else.

    Use what comes with your compiler as a starting point, understand the fundamentals of threading, play around with simple applications and once you get a better understanding you can start looking at alternative (and more platform-independent) ways of threading.

    Don't start running before you can walk....look at the following for example:

    Honestly, I find the WinAPI approach to almost everything to be less intuitive than the alternatives, and that includes threading. I would suggest pthreads as being the simplest way to begin the learning process.

  8. #8
    Join Date
    Apr 2010
    Posts
    3

    Re: Console Multithreading

    OpenMP seems to be very limited to integer variables in areas... I'll try to figure out the Windows MFC one again I guess.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Console Multithreading

    The Boost.Thread library is not a header only, so it's not exactly easy to use. You can find binaries on the web, though, so if they work for you, it'll still be doable even if you have never used multiple libraries for one application before.

    I find the interface of Boost.Thread very easy to use, though. The synchronization primitives in there allow you to do exactly what you want with very little effort.

    I'd say the Win32 API offers you the bare minimum you need for multi-threading plus the Windows specific things, such as thread priorities. You don't get things like thread classes, scoped locks, condition variables or futures. Those are the things that make multi-threading in C++ a lot easier.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: Console Multithreading

    Honestly, I find the WinAPI approach to almost everything to be less intuitive than the alternatives, and that includes threading.
    ...however (just to let know that not everybody here completely shares this opinion ), without firm understanding of what happens underneath the library isolation level we finally get the coder who becomes practically helpless been deprived of the tool due to project (or platform) specifics. And getting from the doping-but-intuitive tool back to bare OS bones is always much and much painful.

    Besides, I would be very careful with "intuitive". What looks pretty much intuitive to experienced person typically hardly can be digested by beginner.
    Best regards,
    Igor

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)