Buran Motion Planning Framework
count_down_latch.h
1 #pragma once
2 
3 #include <iostream>
4 #include <mutex>
5 #include <chrono>
6 #include <condition_variable>
7 #include <thread>
8 
9 namespace bmpf {
14  struct CountDownLatch {
15  volatile int _rest;
16  std::mutex _mtx;
17  std::condition_variable _cv;
18 
19  explicit CountDownLatch(int n) : _rest(n) {}
20 
21  void await() {
22  std::unique_lock<std::mutex> lck(_mtx);
23  while (_rest > 0) {
24  _cv.wait(lck);
25  // std::cout << "Wake up " << std::this_thread::get_id() << std::endl;
26  }
27  }
28 
29  void countDown() {
30  std::unique_lock<std::mutex> lck(_mtx);
31  --_rest;
32  // std::cout << "Notify " << std::this_thread::get_id() << std::endl;
33  _cv.notify_all();
34  }
35 
36  };
37 
38 //void exec(CountDownLatch *a) {
39 // std::this_thread::sleep_for(std::chrono::seconds(2));
40 // a->countDown();
41 //}
42 //int main()
43 //{
44 //
45 // CountDownLatch a(3);
46 // std:: thread threads[3];
47 // // spawn 3 threads:
48 // for (int i=0; i<3; ++i)
49 // threads[i] = std::thread(exec,&a);
50 //
51 // std::cout << "3 threads ready to race...\n";
52 // a.await();
53 // std::cout << "Came to finish\n";
54 //
55 // for (int i=0; i<3; ++i)
56 // threads[i].join();
57 //
58 // return 0;
59 //}
60 
61 }
bmpf::CountDownLatch
Definition: count_down_latch.h:14