Back to TILs

C++ condition_variable_04

Date: 2024-02-19Last modified: 2024-02-26

Table of contents

  std::thread driver_thread( keep_moving );
  std::thread passenger_thread( ask_driver_to_wake_u_up_at_right_time );
  // std::thread passenger_thread_old( set_the_alarm_and_take_a_nap );

  // Random wake up
  std::this_thread::sleep_for( std::chrono::seconds( 3 ) );
  condition_var.notify_one();

  // passenger_thread_old.join();
  passenger_thread.join();
  driver_thread.join();

Possible output

driver: keep_moving... 0
condition_var: notification received, checking
driver: keep_moving... 1
driver: keep_moving... 2
condition_var: notification received, checking
driver: keep_moving... 3
driver: keep_moving... 4
driver: keep_moving... 5
driver: keep_moving... 6
driver: keep_moving... 7
driver: keep_moving... 8
driver: keep_moving... 9
driver: emit notification
condition_var: notification received, checking
Finally I am there, distance covered = 10

References