title: Can someone Explain Mutex and how it is used?
body: <p>I read a few documents about Mutex and still the only Idea I have got is that it helps preventing threads from accessing a resource that is already being used by another resource.</p>
  
  <p>I got from Code snippet and executed which works fine:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;process.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  
  BOOL FunctionToWriteToDatabase(HANDLE hMutex)
  {
      DWORD dwWaitResult;
      // Request ownership of mutex.
      dwWaitResult = WaitForSingleObject(
      hMutex, // handle to mutex
      5000L); // five-second time-out interval
          switch (dwWaitResult)
          {
          // The thread got mutex ownership.
              case WAIT_OBJECT_0:
              __try
              {
                  // Write to the database.
              }
              __finally {
              // Release ownership of the mutex object.
              if (! ReleaseMutex(hMutex)) {
              // Deal with error.
          }
              break;
          }
              // Cannot get mutex ownership due to time-out.
              case WAIT_TIMEOUT:
              return FALSE;
              // Got ownership of the abandoned mutex object.
              case WAIT_ABANDONED:
              return FALSE;
          }
      return TRUE;
  }
  
  void main()
  {
      HANDLE hMutex;
  
      hMutex=CreateMutex(NULL,FALSE,"MutexExample");
  
      if (hMutex == NULL)
      {
          printf("CreateMutex error: %d\n", GetLastError() );
      }
      else if ( GetLastError() == ERROR_ALREADY_EXISTS )
          printf("CreateMutex opened existing mutex\n");
  
      else
          printf("CreateMutex created new mutex\n");
  
  }
  </code></pre>
  
  <p>But What I don't understand is where is the thread and where is the shared resource? Can anyone please explain or provide a better article or document?</p>