title: How to prevent multiple threads to use singleton class instance at same time in cpp
body: <p>I am writting a thread safe singleton class as follows. The following implementation ensures that only one instance of the class in created. My use case is that I am using this instance in a multi thread environment where each thread may call <code>getInstance()</code> and do some work using the instance. My question is how can I ensure that only one thread is using the instance at a particular time, so as to prevent race conditions that might occur if multiple threads are trying to use the single instance at same time.</p>
  
  <pre><code>class Singleton {
      Singleton() {}
      Singleton(const Singleton&amp;) = delete;
      Singleton&amp; operator=(const Singleton&amp;) = delete;
   public:
      static Singleton&amp; getInstance() {
          static Singleton s;
          return s;
      }
  };
  </code></pre>