Let's consider the following code:
#include <iostream>
#include <thread>
int main()
{
std::thread t1([](){
int a;
std::cin >> a;
std::cout << "T1: " << a << std::endl;
});
std::thread t2([](){
int a;
std::cin >> a;
std::cout << "T2: " << a << std::endl;
});
t1.join();
t2.join();
return 0;
}
Compiled with: g++ -std=c++14 -pthread main.cpp -o main
Run with: ./main < file.txt
file.txt:
1 2
The outputs are different ( I supposed so), for example:
T1: T2: 12
T1: 1T2:
2
T1: 12
T2: 0
and so on.
I suppose that code causes undefined behaviour? Am I right? And how to solve/explain situation when two threads try use the same ( ? ) input/output?