On Monday, January 30, 2023 at 9:04:19 PM UTC, 20cs wrote:
> Hii ,
> How can we assure that only one object is created for a class. And when second do not happen.
>
> Note - don't use counter .
>
> Someone asked me this in an interview.
I'd probably have a global object "std::optional<SomeClass> g_object", and I'd protect it with an "std::recursive_mutex".
Any thread can 'emplace' or 'reset' it as they like so long as they lock the mutex first.