In C++, how do I create and use mutex?

In a C++ implementation file I normally use:

// *.cxx

#include <mutex>

namespace {
  std::mutex g_lock;
} // anonymous namespace

void MyClass::myThreadSafeMethod()
  // do something that is not thread safe

Sometimes it is useful to only allow one instance (process) of a program to run, to do this you can use:

bool MyClass::startSingleInstance(const std::string & mutexName)
#ifdef __linux__
// linux?
  HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, 0, mutexName.c_str());
  if (hMutex)
    return false; // This mutex is already running...
  hMutex = CreateMutex(NULL, FALSE, mutexName.c_str());
  DWORD dwError = GetLastError();
  if(dwError != 0)
    throw std::runtime_error("Unknown errors detected while creating mutex: '" + mutexName + "'");
  return true;