title: What happens without a binary semaphore
body: <p>Assume the code below demonstrating a binary semaphore example.</p>
  <p>In this example, we have a pthread that reads the source.txt and attempts to copy the content to destination.txt while locking it using a binary semaphore.</p>
  <p>What happens in the comment section below without the semaphore?</p>
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;memory&gt;
  #include &lt;filesystem&gt;
  #define _TIMESPEC_DEFINED
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;thread&gt;
  #include &lt;valarray&gt;
  
  pthread_t StartFileAccessThread();
  void *CopyFile(void *parameters);
  
  int main(int argc, char* argv[])
  {
      pthread_t thread = StartFileAccessThread();
  
      void *returnValue;
      pthread_join(thread, &amp;returnValue);
  
      getchar();
      return EXIT_SUCCESS;
  }
  
  
  pthread_t StartFileAccessThread()
  {
      std::string sourcePath(&quot;source.txt&quot;);
      std::string destinationPath(&quot;dest.txt&quot;);
      sem_t semaphore;
      sem_init(&amp;semaphore, 0, 0);
      pthread_t thread;
  
      void *parameters[3];
      parameters[0] = &amp;semaphore;
      parameters[1] = &amp;sourcePath;
      parameters[2] = &amp;destinationPath;
  
      pthread_create(&amp;thread, nullptr, &amp;CopyFile, parameters);
  
      // What happens without the binary semaphore?
      sem_wait(&amp;semaphore);
      sem_destroy(&amp;semaphore);
  
      printf(&quot;Freeing ressources.\n&quot;);
  
      return thread;
  }
  
  
  void *CopyFile(void *rawParameter)
  {
      void **parameters = static_cast&lt;void **&gt;(rawParameter);
  
      sem_t *semaphore = static_cast&lt;sem_t *&gt;(parameters[0]);
      std::string sourcePath(*static_cast&lt;std::string *&gt;(parameters[1]));
      std::string destinationPath(*static_cast&lt;std::string *&gt;(parameters[2]));
  
      sem_post(semaphore);
  
      std::this_thread::sleep_for(std::chrono::seconds(2));
  
      copy_file(sourcePath, destinationPath, std::experimental::filesystem::copy_options::overwrite_existing);
  
      printf(&quot;File copied \n&quot;);
  
      return nullptr;
  }
  </code></pre>