troubleshooting Question

derived class + mutex question

Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox asked on
C++
4 Comments1 Solution563 ViewsLast Modified:
Hi,

This is all psedocode. I have a POSIX mutex I want to keep as a member of my class. This is working fine so far:

    class CFactory {
        void Produce();
        mutex m_MyMutex;
    };

so when a user tries using Produce() on the same factory instance it will be synchronized between users:

    CFactory::CFactory()
    {
         InitMutex(m_MyMutex);
    }

    void CFactory::Produce()
    {
        EnterMutex(m_MyMutex);
        // ...
        ExitMutex(m_MyMutex);
    }

    int main()
    {
        CFactory f;
        for (int i = 0 ; i < 100; i++) {
             MakeThread();
             EachThreadCalls(f.Produce()); // they'll each have to wait for the lock to be released before they can finish the Produce() method.
        }
     }

So this is good, but what happens now if I derive a class from CFactory? Can it still use the m_MyMutex member for its own specialized Produce methods? Do I have to initialize the mutex in its constructor again since the derived class won't inherit the constructor?

     class CFactoryForCars : public CFactory {
         void ProduceACar();
     };

Thanks
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros