Advertisement
khaze1

store

Oct 18th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. class Store {                                                  
  2. public:      
  3.     std::shared_ptr<Bread> GetBread() {
  4.         mut.lock();                    
  5.         ThreadChecker checker(count_);
  6.         ++next_id_;                  
  7.         mut.unlock();
  8.         return std::make_shared<Bread>(next_id_);
  9.     }                                            
  10.      
  11.     std::shared_ptr<Sausage> GetSausage() {
  12.         mut.lock();                        
  13.         ThreadChecker checker(count_);
  14.         ++next_id_;                  
  15.         mut.unlock();
  16.         return std::make_shared<Sausage>(next_id_);
  17.     }                                              
  18.      
  19. private:
  20.     int next_id_ = 0;
  21.     std::atomic_int count_ = 0;
  22.     std::mutex mut;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement