I want to run 2 loop at the sime time without having to wait for one to finish before starting the other. What I did works on WINDOWS but it doesn't on LINUX and I don't know why. When I compile the code simply with : g++ -o A A.cpp and then I run it, it just does nothing and it doesn't display anything on the standard output. On Windows id displays something like: 12121221212222112121212121121212
void f1()
{
for (int i = 0; i < 500; ++i)
{
std::cout << "1";
std::this_thread::sleep_for(1s);
}
}
void f2()
{
for (int i = 0; i < 500; ++i)
{
std::cout << "2";
std::this_thread::sleep_for(1s);
}
}
int main()
{
std::thread worker(f1);
std::thread worker2(f2);
int a;
std::cin >> a;
return 1;
}
Maybe I'm missing something about LINUX environment and I have to set some parameters in the thread obj. Maybe in LINUX i have to do in another way. I'd like to know if this is something that should work on a Linux and I'm just missing something, or if I have to use another approach.