vendredi 29 juillet 2016

Why console app hangs when using a shared dll that containg static variable that use mutex?

I have a shared dll library that contains a class as below :

inside A.dll >> Header File :

class API ErrorHandler  
{
public:
    ErrorHandler();

    virtual ~ErrorHandler();
protected:  
    static ErrorHandler* defaultHandler();

private:
    static ErrorHandler* _pHandler;
    static std::mutex     _mutex;
};

source(.cpp)

ErrorHandler* ErrorHandler::_pHandler = ErrorHandler::defaultHandler();
std::mutex ErrorHandler::_mutex;


ErrorHandler::ErrorHandler()
{
}


ErrorHandler::~ErrorHandler()
{
}

ErrorHandler* ErrorHandler::defaultHandler()
{   
    static SingletonHolder<ErrorHandler> sh;
    return sh.get(); **<<====== here we get hanged** see the declaration of get
}

SingletoneHolder header file

template <class S>
class SingletonHolder
{
public:
    SingletonHolder():
        _pS(0)
    {
    }

    ~SingletonHolder()
    {
        delete _pS;
    }

    S* get()
    {
        std::lock_guard<std::mutex> lock(_m); <===== cause thread hang
        if (!_pS) _pS = new S;

        return _pS;
    }

private:
    S* _pS;
    std::mutex _m;
};

After building the above code (every thing related to compiler setting configured correctly) now I want to use it in my console app.

After running console app, app hangs and never reach to main function.

Why std::lock_guard<std::mutex> lock(_m); hangs and prevent main thread to continue executing?

What is alternative?

I am using VS2013 Update5.

Aucun commentaire:

Enregistrer un commentaire