Thread: Boost threads; how do I get a thread to yield after a specified amount of time

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's simple: since standard C++ currently doesn't even acknowledge the existence of threads, the standard makes no mention of them whatsoever, nor of thread safety issues. The thread safety of components is therefore completely up to the implementation.

    Apparently MS's implementation is thread-safe (or as thread-safe as the shared-state cout can possibly be), while GCC's is 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

  2. #17
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by pheres View Post
    one thing to add: never use cout from parallel threads, because it's not thread safe. you should prefer printf in this case.
    But if a thread obtains a lock on the Mutex associated with IO (locking before it uses cout, and unlocking afterwards) which is a shared resource, then the code becomes thread safe, correct?

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by indigo0086 View Post
    But if a thread obtains a lock on the Mutex associated with IO (locking before it uses cout, and unlocking afterwards) which is a shared resource, then the code becomes thread safe, correct?
    That's sort of the point. For a library function/object to be thread safe, it would need to function correctly even if the code using it does not protect access to it. cout is not thread safe so, if it is to be used from multiple threads, it must be protected.

    It is often very difficult (sometimes impossible) for a library function or object (which is logically represented by a set of interacting functions that share some data) to protect itself against access from multiple threads. So the responsibility for serialising access falls on the code which uses it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clock()
    By C_ntua in forum C Programming
    Replies: 19
    Last Post: 10-08-2008, 11:45 AM
  2. Determing the amount of change(money)
    By Kyeong in forum C Programming
    Replies: 11
    Last Post: 09-30-2008, 04:36 PM
  3. How to resume thread at the specific time instance?
    By qingxing2005 in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2007, 12:28 AM
  4. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 09:40 AM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM