title: Which synchronization primitive should I employ here?
body: <pre class="lang-c prettyprint-override"><code>while(1) {
       char message_buffer[SIZE];
       ssize_t message_length = mq_receive(mq_identifier, message_buffer, _mqueue_max_msg_size NULL);
  
       if(message_len == -1) { /* error handling... */}
  
       pthread_t pt1;
       int ret = pthread_create(&amp;pt1, NULL, handle_message, message_buffer);
       if(ret) { /* error handling ... */}
  }
  
  void * handle_message (void * message) {
      puts((char *) message);
      return NULL;
  }
  </code></pre>
  
  <p>The above example is not an MRE but it is extremely simple:</p>
  
  <p>I've got a main thread with a loop that constantly consumes messages from a message queue. Once a new message is received, it is stored in the local <code>message_buffer</code> buffer. Then, a new thread is spawned to "take care" of said new message, and thus the message buffer's address is passed into <code>handle_message</code>, which the new thread subsequently executes. </p>
  
  <hr>
  
  <h1>The problem</h1>
  
  <p>Often, 2 threads will print the same message, even though I can verify with a 100% certainty that the messages in the queue were not the same.</p>
  
  <hr>
  
  <p>I am not completely certain, but I think I understand why this is happening: </p>
  
  <p>say that I push 2 different messages to the mqueue and only then I begin consuming them.</p>
  
  <p>In the first iteration of the <code>while</code> loop, the message will get consumed from the queue and saved to <code>message_buffer</code>. A new thread will get spawned and the address of <code>message_length</code> passed to it. But that thread may not be fast enough to print the buffer's contents to the stream <em>before</em> the next message gets consumed (on the next iteration of the loop), and the contents of <code>message_buffer</code> subsequently overridden. Thus the first and second thread now print the same value.</p>
  
  <hr>
  
  <p>My question is: what is the most efficient way to solve this? I'm pretty new to parallel programming and threading/pthreads and I'm pretty overwhelmed by the different synchronization primitives.</p>
  
  <h1>Mutex trouble</h1>
  
  <pre class="lang-c prettyprint-override"><code>static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  
  while(1) {
       char message_buffer[SIZE];
       pthread_mutex_lock(&amp;m);
       ssize_t message_length = mq_receive(mq_identifier, message_buffer, _mqueue_max_msg_size NULL);
       pthred_mutex_unlock(&amp;m);
  
       if(message_len == -1) { /* error handling... */}
  
       pthread_t pt1;
       int ret = pthread_create(&amp;pt1, NULL, handle_message, message_buffer);
       if(ret) { /* error handling ... */}
  }
  
  void * handle_message (void * message) {
      char own_buffer[SIZE];
      pthread_mutex_lock(&amp;m);
      strncpy(own_buffer, (char *) message, SIZE);
      pthread_mutex_unlock(&amp;m);
      puts(own_buffer);
      return NULL;
  }
  </code></pre>
  
  <p>I don't think my current mutex implementation is right as the threads are still receiving duplicate messages. The main thread can lock the mutex, consume a message into the buffer, unlock the mutex, spawn a thread, but that thread still may hang  and the main one could just rewrite the buffer again (as the buffer mutex was never locked by the new thread), effectively making my current mutex implementation useless? How do I overcome this?</p>