Original code:
singleclass* singleclass::getinstance ()
{
mutex.lock ()
if (_singleclass = = nullptr)
{
_ Singleclass = new _singleclass ();
}
Mutex.unlock ();
return _singleclass;;
}
Std:::lock_guard<std::mutex> Locker (Parameters (Locker)) >
In the code above because the code only needs to be new once _singleclass ()
So every time you use a lock, you're wasting your resources.
So change the code to look like the following
singleclass* singleclass::getinstance ()
{
if (_singleclass = = nullptr)
{
mutex.lock ()
if ( _singleclass = = nullptr)
_singleclass = new _singleclass ();
}
Mutex.unlock ();
return _singleclass;;
}
This will return directly to _singleclass after creating the completion instance; And no further locks are added.
This will return directly to _singleclass after creating the completion instance; And no further locks are added.