MaximCherchuk

VPTR

Mar 15th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <cstdio>
  2. #include <mutex>
  3. #include <condition_variable>
  4. #include <thread>
  5.  
  6. using namespace std;
  7.  
  8. #define pb push_back
  9. #define mp make_pair
  10.  
  11. typedef long long int ll;
  12.  
  13. class A {
  14. public:
  15.     A() : done_(false) {}
  16.     virtual void F() {
  17.         printf("A::F\n");
  18.     }
  19.     void Done() {
  20.         std::unique_lock<std::mutex> lk(m_);
  21.         done_ = true;
  22.         cv_.notify_one();
  23.     }
  24.    
  25.     virtual ~A() {
  26.         std::unique_lock<std::mutex> lk(m_);
  27.         cv_.wait(lk, [this] {return done_; });
  28.     }
  29. private:
  30.     std::mutex m_;
  31.     std::condition_variable cv_;
  32.     bool done_;
  33. };
  34.  
  35. class B : public A {
  36. public:
  37.     virtual void F() override {
  38.         printf("F::B\n");
  39.     }
  40.     virtual ~B() override {}
  41.    
  42. };
  43.  
  44. int main(int argc, char** argv) {
  45.     A *a = new B();
  46.     thread t1([a] { a->F(); a->Done();});
  47.     thread t2([a] { delete a;});
  48.     t1.join();
  49.     t2.join();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment