7

I'd like to state my problem using an example.

Assuming there is an array of N /*(N>>1)*/ threads which are set to run this function:

void Process() {
    //Some thread safe processing which requires in-deterministic computation time

    unsigned char byte;
    std::cin >> byte;
}

Once all of them are started simultaneously, what does happen? How are concurrent std::cin accesses handled? What does the end-user who operates on console see/experience?

Edit: There is one more thing I'd like to add. Is the code below safe enough to abandon the idea of using std:cin only in one (probably main) thread?

void Process() {
    //Some thread safe processing which requires in-deterministic computation time

    //Mutex lock
    unsigned char byte;
    std::cin >> byte;
    //Mutex unlock
}
3
  • 1
    To be specific you should not avoid these functions in a multithreaded environment alltogether, but you should call them from one thread only.
    – orlp
    Commented Sep 17, 2013 at 7:58
  • I'm aware the safety issue this kind of usages deal with. The reason I'm asking this is to decide a syntax rule (whether to force programmers to use stdin in main thread) for a new SIMD programming language which I implement as a cross compiled C++. Commented Sep 17, 2013 at 8:04
  • @diegoperini: That assumes stdin is somehow special, and that there is an actual main thread. Remember that in C++, std::cin is't really that special. It's just an std::istream&. And you really can't restrict every istream& to the main thread.
    – MSalters
    Commented Sep 17, 2013 at 10:34

3 Answers 3

9

I would say, the result is not predictable without mutexes.

If you use a mutex, everything is fine. That's what mutexes are for.

0
6

Pre C++11, it depends on the implementation; at least one implementation guaranteed synchronization of the calls. (VC++ guarantees the synchronization of std::cout, but not for other iostream objects. g++ offers the same guarantees as C++11, even in earlier versions of the compiler.) In C++11, it is explicitly undefined behavior.

The general rule is simple: any modification of state of an object in any thread requires all accesses to be synchronized. And all >> operators on std::istream are considered to modify its state.

More generally, for any particular stream, it is probably a good policy to use it in only one thread. (There are exceptions, such as logging streams, where the logging object ensures thread safety.) This is especially true of input, since even when synchronized externally, the sequencing of the operators will generally be unspecified if they are in separate threads. So even with synchronization, if your user inputs "ab", which thread gets 'a' and which gets 'b' in your example would be undetermined.

EDIT:

There seem to be special rules for the standard stream objects (std::cin, std::cout, etc.). Concurrent access is guaranteed not to produce a data race, at least in some cases. It may still result in characters being mixed, i.e.: if you're inputing int (rather than a single character), and your user inputs "123 89\n", there's a possibility that thread 1 will see "1389\n", and thread 2 "2 ", which won't give you coherent results.

My global recommendation stands. I can't think of any realistic case where the interleaving of characters wouldn't create a problem. (Also, I don't think I've ever written code which actually inputs from std::cin; it's always from an std::istream& which might be std::cin, or might be a file.)

6

The first thread uses std::cin locks it (Unless you use sync_with_stdio(false)). So, you don't need a mutex. http://en.cppreference.com/w/cpp/io/cin Anyway I don't recommend to use it in multi-thread application. The problem is there's no way to interrupt cin input from other thread.

2
  • Upvoted and will mark down as best unless there is more useful info provided. Commented Sep 17, 2013 at 8:27
  • The standard doesn't say that std::cin locks it; it says that there will be no data race. What this means in practice isn't too clear, but the standard does explicitly say that characters can be interleaved. (No problem when inputting bytes, I suppose, but you could end up with incoherences when inputting anything that might be more than one character.) Commented Sep 17, 2013 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.