jeudi 27 octobre 2022

Sending Condition Varible Signal From Abstarct Base Class to Derived Classes

There is an Abstract Base Class with a static function and and a static variable and with a pure virtual function.

For Each Request update the static variable using static function. And for each update It should be notify the base class condition variable each of them are different from each other.

class Base{
   protected:
      static int ms_Varible;
      static std::mutex m; 
   public:

       static void update(int pVar){
          m.lock();
          ms_Varible = pVar;
          m.unlock();
          if(ms_Varible == 1){notifyDerived1()}
          if(ms_Varible == 2){notifyDerived2()}
          if(ms_Varible == 3){notifyDerived3()}
       }

       std::string toString() = 0;

};


class Derived1 : public Base{

   public:

      void foo(){

        while(true){
        
             wait_cond(cond_varible);
        }

      }

      std::string toString(){return "Derived1";}
};

....

In this scenario where should I keep the condition variables. How Can I notify them in base class.

Is the design could be better? I think the condition variable can be static in the derived classes but it does not seems to right.

Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire