Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) C++11 std::thread example 2: two counting threads using std::mutex

 

This std::thread examples demonstrates how to use std::mutexes to share std::cout nicely, as in the previous example, std::thread example 1: two counting threads, the output to std::cout was messy.

 

 

#include <iostream>
#include <thread>

void DoCountDown(const int id)
{
  //static mutex, because each thread its must use the same mutex
  static std::mutex mutex;

  for (int i=0; i!=10; ++i)
  {
    ///Let this thread sleep, to give the other thread a chance
    std::this_thread::sleep_for(std::chrono::milliseconds(1));

    ///Acquire access to std::cout
    std::lock_guard<std::mutex> lock(mutex);

    ///Write to std::cout
    std::cout << "#" << id << ": " << (10-i) << '\n';
  }
}

int main()
{
  std::thread t1(DoCountDown,1);
  std::thread t2(DoCountDown,2);
  t1.join();
  t2.join();
}

 

Screen output:

 

#2: 10
#1: 10
#2: 9
#1: 9
#2: 8
#1: 8
#2: 7
#1: 7
#2: 6
#1: 6
#2: 5
#1: 5
#2: 4
#1: 4
#2: 3
#1: 3
#2: 2
#1: 2
#2: 1
#1: 1

 

This example is checked for errors in helgrind example 2: two counting threads using std::mutex, where helgrind is shown to detect no errors.

 

 

 

 

 

References

 

  1. Bjarne Stroustrup's C++0x page

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict