title: C++ - Multithreading takes longer with more threads
body: <p>I'm making a parallel password cracker for an assignment. When I launch more than one thread, the times taken to crack take longer the more threads I add. What is the problem here? </p>
  
  <p>Secondly, what resource sharing techniques can I use for optimal performance too? I'm required to use either mutexes, atomic operations or barriers while also using semaphores, conditional variables or channels. Mutexes seem to slow my program down quite drastically.</p>
  
  <p>Here is an example of my code for context:</p>
  
  <pre><code>std::mutex mtx;
  std::condition_variable cv;
  
  void run()
  {
    std::unique_lock&lt;std::mutex&gt; lck(mtx);
    ready = true;
    cv.notify_all();
  }
  
  crack()
  {
    std::lock_guard&lt;std::mutex&gt; lk(mtx);
    ...do cracking stuff
  }
  
  main()
  {
    ....
  
    std::thread *t = new std::thread[uiThreadCount];
  
    for(int i = 0; i &lt; uiThreadCount; i++)
    {
      t[i] = std::thread(crack, params);
    }
  
    run();
  
    for(int i = 0; i &lt; uiThreadCount; i++)
    {
      t[i].join();
    }
  
  }
  </code></pre>