0

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.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    try calling join on the threads befor returning from main. You might also have a look at std::async/std::future (if in the future you want to return results from threads) – Pepijn Kramer Oct 27 '22 at 06:45
  • Hi, I've already tried with: worker.join(); worker2.join(); but still it doesn't output anything. If you try this code on your linux machine does it works? thanks – Joshua Brombt Oct 27 '22 at 06:47
  • 1
    When the `main` functions returns, the whole process exits as well, killing all thread in the process. – Some programmer dude Oct 27 '22 at 06:47
  • Try this : `std::cout << "2" << std::flush;` I noticed sometimes on linux that helps. – Pepijn Kramer Oct 27 '22 at 06:49
  • 1
    Also note that almost all environments consider a return of **`0`** from the `main` function to mean "success", and any small positive integer as "failure". – Some programmer dude Oct 27 '22 at 06:49
  • 1
    On Windows, when the main thread exits, other threads keep running. On Linux, when the main thread exits the entire process (including other threads) terminates. – Jesper Juhl Oct 27 '22 at 06:49
  • 1
    Consider using the `EXIT_SUCCESS` & `EXIT_FAILURE` macros as your return value from `main` rather than a raw hardcoded integer, for greater readability and portability. – Jesper Juhl Oct 27 '22 at 06:53
  • 'std::flush' at the end of the std::cout help me !! THANK YOU!@pepijn-kramer – Joshua Brombt Oct 27 '22 at 06:54
  • Please don't edit answers into your question – Alan Birtles Oct 27 '22 at 07:18

1 Answers1

0

You are adding "1"s and "2" to the stdout buffer, it might take a while before you see any output.

See: https://stackoverflow.com/a/1716621

fprintf(stdout,"yourmessage") == std::cout << "yourmessage"

The stdout stream is line buffered by default so will only display what's in the buffer after it reaches a newline

Maybe adding a "\n" at every std::cout might help.

Your threads look okay, but its good practice to .join() them when their task is done. Calling .join() will pause your the main() thread but thats what the std::cin is for, right ?

Also, I would not flush after every 1/2, flushing is quite expensive.

idk
  • 11
  • 3