title: C++ MultiThreading Mutex Locks Segmentation Fault
body: <p>** This is for a college class, I am not actually trying to crack passwords **
  Below is my source code, but essentially what I want to have happen is the parent process en queues passwords into an std::list&lt;> attemptList. Then child threads grab from the front of the queue and, currently, just print out the value.</p>
  
  <p>As you can see by the code below I try using std::mutex to block the attemptList when the front is being popped, but both threads are blowing past the lock at the same time and reading from the front. Then one of them segmentation faults and the program crashes. </p>
  
  <p>The specific code section that is seg faulting is...</p>
  
  <pre class="lang-cpp prettyprint-override"><code>mutex.lock();
  password = attemptList.front();
  attemptList.pop_front();
  size = attemptList.size();
  std::cout &lt;&lt; password &lt;&lt; std::endl;
              mutex.unlock();
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;chrono&gt;
  #include &lt;shared_mutex&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;mutex&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;list&gt;
  
  #define MAX_LENGTH 4
  #define MAX_QUEUE_SIZE 1000
  #define CHARACTER_LIST "abcdefghijklmnopqrstuvwxyz"
  
  void enqueue_passwords(const std::string&amp; charList);
  void bruteforce();
  void do_join(std::thread&amp; t);
  void join_all(std::vector&lt;std::thread&gt;&amp; v);
  
  std::list&lt;std::string&gt; attemptList;
  std::mutex mutex;
  bool conclude = false;
  
  int main(int argc, char* argv[]) {
      auto start = std::chrono::high_resolution_clock::now();
  
      int index;
      std::vector&lt;std::thread&gt; threads;
      for (index = 0; index &lt; 2; index++) {
          threads.emplace_back(std::thread(bruteforce));
      }
  
      enqueue_passwords(CHARACTER_LIST);
  
      join_all(threads);
  
      auto stop = std::chrono::high_resolution_clock::now();
      auto duration = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(stop - start);
      std::cout &lt;&lt; duration.count() &lt;&lt; " milliseconds" &lt;&lt; std::endl;
  
      return 0;
  }
  
  void bruteforce() {
      double size = 0;
      std::string password;
  
      while (!conclude) {
          do {
              mutex.lock();
              size = attemptList.size();
              mutex.unlock();
  
              if (size == 0) {
                  usleep(300);
              }
          } while (size == 0);
  
          while(size != 0) {
              mutex.lock();
              password = attemptList.front();
              attemptList.pop_front();
              size = attemptList.size();
  
              std::cout &lt;&lt; password &lt;&lt; std::endl;
              mutex.unlock();
          }
      }
  }
  
  void enqueue_passwords(const std::string&amp; charList) {
      const int maxLength = MAX_LENGTH;
      const int charListLength = charList.length();
  
      char password[MAX_LENGTH + 1];
      memset(password, '\0', MAX_LENGTH + 1);
  
      int index;
      int number;
  
      double permutations = 0;
  
      double count = 0;
      double passPosition = 0;
      double size = 0;
  
      // Calculate number of permutations possible
      for (index = 0; index &lt; maxLength; index++) {
          permutations += charListLength * powl(charList.length(), maxLength - index - 1);
      }
  
      std::cout &lt;&lt; "Permutations:  " &lt;&lt; permutations &lt;&lt; std::endl &lt;&lt; std::endl;
  
      password[0] = charList[0];
      while (count &lt; permutations) {
          do {
              mutex.lock();
              size = attemptList.size();
              mutex.unlock();
  
              if (size &gt; MAX_QUEUE_SIZE) {
                  usleep(250);
              }
          } while (size &gt; MAX_QUEUE_SIZE);
  
          // Loop over current set of characters ,changing the last one
          for (index = 0; index &lt; charListLength; index++) {
              password[int(passPosition)] = charList[index];
  
              // ENQUEUE HERE //
              mutex.lock();
              attemptList.push_back(std::string(password));
              mutex.unlock();
              // ENQUEUE HERE //
  
              if (count &gt; permutations) {
                  break;
              }
              count++;
          }
  
          // Iterate over remaining indexes, except for the last one
          for (number = int(passPosition); number &gt;= 0; number--) {
              if (password[number] != charList[charListLength - 1]) {
                  password[number]++;
                  break;
              } else {
                  if (number == 0) {
                      passPosition++;
                      for (index = 0; index &lt; passPosition + 1; index++) {
                          password[index] = charList[0];
                      }
                      break;
                  }
                  password[number] = charList[0];
              }
          }
      }
  
      conclude = true;
  }
  
  void do_join(std::thread&amp; t) {
      t.join();
  }
  
  void join_all(std::vector&lt;std::thread&gt;&amp; v) {
      std::for_each(v.begin(), v.end(), do_join);
  }
  </code></pre>