Thread: c++11 threads question

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    c++11 threads question

    I always considered threads to be operating system oriented , where you can't simply "invade" the system inner processes and force them to execute code in a parallel fashion , you have to ask the OS to do so, if it's an allowed and given API.
    Languages like Java have system-suited VM where they can implement threads in each OS using it's given API (as win32api , etc.).

    just out of curiosity , how c++11, which has a portable code implements OS-independent threads?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    They have a standardized memory model, and the standard library handles the wrapping of the OS-specific portions. This is exactly the same thing that Java and .Net do.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Just like the new keyword is used to call operating system specific heap allocation routines, so thread library functions will call operating system specific functions to launch and handle threads.

    C++ doesn't have a VM, so those operating system calls are part of the compiled executable. This is why C++ executables are not portable.
    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
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    If you use dynamic linking the executable is portable as far as the standard library handling the OS calls because it will be linked in on whatever system you transfer the binary too. The reason it's not portable across systems like windows and linux is because the executable format itself is different on across those systems. The source is of course still portable though and can simply be recompiled on the new system to gain a locally compatible executable (or use cross-compiling on a single host).

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    So , as I can see it the C++ committee only implement the logic of the threads, and it's up to compiler developers to implement specific OS linking/compiling things?

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Dave11 View Post
    So , as I can see it the C++ committee only implement the logic of the threads, and it's up to compiler developers to implement specific OS linking/compiling things?
    I would wager, yes. This is in the same line of reasoning as King Mir and Elkvis.

    Heap allocations are OS-specific too so adding threads to the list is in the same ballpark.

    That being said, I love, love, love C++11 for introducing std::thread and lambdas. It makes threading an application so simple I can't help but fall in love.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave11 View Post
    So , as I can see it the C++ committee only implement the logic of the threads, and it's up to compiler developers to implement specific OS linking/compiling things?
    Yes. The committee only describes what shall happen and produces a document that describes that. It's up to the compiler developers and standard library developers to make sure that actually happens.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 12-14-2012, 11:00 AM
  2. A question about using threads
    By Roddman in forum C Programming
    Replies: 2
    Last Post: 03-25-2008, 09:15 AM
  3. Threads question
    By tezcatlipooca in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2007, 05:10 AM
  4. Simpile question on threads
    By Josh Kasten in forum Windows Programming
    Replies: 4
    Last Post: 07-26-2003, 04:09 AM
  5. Question about threads
    By revelation437 in forum C++ Programming
    Replies: 13
    Last Post: 06-11-2003, 03:17 PM