Solution: Yes, it looks good.
Additional Information:
If it is acceptable for you to potentially have two instances for a short period of time, where the second will be immediately destroyed, you can get rid of the mutex:
std::atomic<S *> S::_instance;
S& S::Instance()
{
using namespace std;
auto tmp = _instance.load(memory_order_relaxed);
if (tmp == nullptr)
{
auto tmp2 = new S();
if( !_instance.compare_exchange_strong(tmp, tmp2) )
delete tmp2;
}
return *tmp;
}
void S::Dispose()
{
using namespace std;
auto tmp = _instance.load(memory_order_relaxed);
if (tmp != nullptr)
{
if( _instance.compare_exchange_strong(tmp, nullptr) )
delete tmp;
}
}
(), a nullptr S. , S.
, Scott Meyers, :
S& S::Instance()
{
static Singleton instance;
return instance;
}
, , btw.