c++ get factory method to be thread safe

89 Views Asked by At

i have this c++ simple factory method :

class Vehicle {
public:
    virtual void printVehicle() = 0;
    static Vehicle* Create(VehicleType type);
    virtual ~Vehicle(){}
};
    Vehicle* Vehicle::Create(VehicleType type) {
        if (type == VT_TwoWheeler)
            return new TwoWheeler();
        else if (type == VT_ThreeWheeler)
            return new ThreeWheeler();
        else if (type == VT_FourWheeler)
            return new FourWheeler();
        else return NULL;
    }

I want that this factory method will when called from more then 1 thread be protected and return New Object to the caller thread only . other threads will wait in java i guess its something like :

synchronized (Vehicle .class) {
   .. protected code ...
}

How can it done in c++14 ?

0

There are 0 best solutions below