3

Ok I'm learning multi-threads in c++11 using Mac. As far as I know that all threads are executed simultaneously. I found the following code from here

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread

void foo() 
{
    std::cout << "\nIn foo \n";
}

void bar(int x)
{
    std::cout << "\nIn bar \n";
}

int main() 
{
    std::thread first (foo);     // spawn new thread that calls foo()
    std::thread second (bar,0);  // spawn new thread that calls bar(0)

    std::cout << "main, foo and bar now execute concurrently...\n";

    // synchronize threads:
     first.join();                // pauses until first finishes
    second.join();               // pauses until second finishes

    std::cout << "foo and bar completed.\n";

    return 0;
} 

Every time I run the code, I get weird results as the following sample

m

aIIinnn ,bf aofroo o

and bar now execute concurrently... foo and bar completed.

what am I missing?

CroCo
  • 5,531
  • 9
  • 56
  • 88
  • 3
    THIS IS NOT A DUPLICATE OF THE LINKED ANSWER. Despite of titles, the OP here is not asking a generic question about std::cout: he's asking about a specific problem that can lead to specific answers (as in fact are the one proposed before the closing). Does not deserve to be closed as duplicate. At least not in the way it was. – Emilio Garavaglia Jan 06 '14 at 07:11

3 Answers3

4

You are printing via std::cout from two threads at once. While this is apparently not a data race, and thus "safe" [*], the output it -- as you can see -- not particularly meaningful. You probably want to use an std::mutex to restrict access, or to use an IO library that provides stronger guarantees.

[*] unless you called std::cout.sync_with_stdio(false)

Cactus Golov
  • 3,474
  • 1
  • 21
  • 41
3

It is because both of the threads are writing on the standard output std::out at the same time. You will need to add thread synchronization mechanisms (mutex, semaphores, etc) to make sure that only one thread writes to the std::out at a time. The problem you are facing is due to the fact that threads run parallel to each other, thats their purpose. Search for mutex or semaphore and integrate them into your code and you will solve the issue.

If you run your current code multiple time, you will see that the result will vary each time you execute the code, or at least most of the times.

Kamran Khan
  • 1,367
  • 1
  • 15
  • 19
2

The code has two parts. The first part is thread first, thread second, and main running concurrently (i.e., at the same time). As a result, the printing of "\n In foo \n", "\n In bar \n" and "main, foo and bar now execute concurrently...\n" will happen simultaneously and thus the output will be random mixture of them (more formally, it is called racing).

In other words, the first part prints "the random output" mentioned in OP, which is the shuffle of "\n In foo \n", "\n In bar \n" and "main, foo and bar now execute concurrently...\n".

After the first part, main thread will pause until first thread and second thread finish (known as synchronization.) As a result, the printing of "foo and bar completed.\n" will always generate exact output.

keelar
  • 5,814
  • 7
  • 40
  • 79