Thread: Threading in C++

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Threading in C++

    A quick search was not pulling up anything I could use as a starting point. If I wanted to learn threading as I know it in Java or c#, what resource and where should I look for c++ multithreading?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    this site has all the info you would need.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Beware that unlike Java and C#, it's very important in C++11 to make all threaded code that uses the standard library const correct.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    Beware that unlike Java and C#, it's very important in C++11 to make all threaded code that uses the standard library const correct.
    Why ?
    (I mean, other than "it's very important in C++ to make all code const correct.")
    I can imagine edge cases like captured objects, but is there a more general reason ?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Const correct code is thread safe in c++11, see here:C++ and Beyond 2012: Herb Sutter - You don't know [blank] and [blank] | Channel 9
    Last edited by Shakti; 01-21-2013 at 08:52 AM.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]Sorry for the needlessly aggressive original. I've rewritten the post with more context.[/Edit]

    Const correct code is thread safe in c++11
    Yog Sothoth!

    Will you please never repeat only half of that discussion again? It is hard enough to teach `const' and `mutable'.

    Your statement lacks crucial context.

    For that to be true, `const' operations MUST be composed of only read operations across threads or be made thread-safe using measures internal to the operation.

    If neither of those holds true for a given class, it is, in fact, trivial to write a class that is `const'-correct and `mutable'-correct. Consider a class that caches to a `mutable' variable being read from a `const' method: from the point of view of the `const' `this' the `mutable' variable is a read operation even though a second thread, with a reference to the same `this', may use an operation that writes to the `mutable' variable. For the standard library guarantees to hold, the reads and writes to that `mutable' variable must be internally atomic so that they outside world really does get to assume that such operations are thread-safe making the implementation, of the standard library, thread-safe by virtue.

    There is no magic to this "new" situation; if you write internals of a class such that they aren't thread-safe with respect to `const' and `mutable' using those keywords correctly will do nothing for you. The standard library implementation in question will probably choke on your class.

    Soma
    Last edited by phantomotap; 01-21-2013 at 11:07 AM.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Yah that was a wreckless comment, hindsight is 20/20....should have dumped the link only!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  2. Threading
    By nc3b in forum C++ Programming
    Replies: 7
    Last Post: 11-18-2005, 02:06 AM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. Threading
    By slcjoey in forum C# Programming
    Replies: 2
    Last Post: 03-24-2005, 01:56 AM
  5. Threading
    By vasanth in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-18-2004, 04:55 PM