title: Do pthread mutexes in a single-threaded process need to be re-initialized on fork()?
body: <h1>Preamble</h1>
  <p>The marked &quot;dupe&quot; above does <strong>not</strong> answer my question, as it involves the safety of forking with threads in use. I am not generating threads in my code, and am more concerned with the validity of <code>pthread_mutex_t</code> structs and their internals registered with the OS when a <code>fork()</code> occurs<a href="https://stackoverflow.com/questions/4597893/specifically-how-does-fork-handle-dynamically-allocated-memory-from-malloc">.</a>  ie: <strong>Are the mutexes re-created on <code>fork()</code> within the child process, or do children just have a valid (or invalid) shallow copy of the parent's mutex internals?</strong></p>
  <hr />
  <h1>Background</h1>
  <p>I have an audio/hardware handling library that wraps some DSP functions with a simple API using a recursive <code>pthread_mutex_t</code>. The reason it is a recursive mutex is because some API functions call other API functions in turn, and I want to make sure only a single thread ever enters the critical section per instance of the library. So, the code would look like so:</p>
  <pre><code>static pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  
  void read() {
      pthread_mutex_lock(&amp;mutex);
      // ...
      pthread_mutex_unlock(&amp;mutex);
  }
  
  void write() {
      pthread_mutex_lock(&amp;mutex);
      // ...
      pthread_mutex_unlock(&amp;mutex);
  }
  
  void toggle() {
      pthread_mutex_lock(&amp;mutex);
      read();
      // ...
      write();
      pthread_mutex_unlock(&amp;mutex);
  }
  </code></pre>
  <hr />
  <h1>Question</h1>
  <p>If a user application uses my library, and the application issues the <code>fork()</code> call, does the child process's instance of my library need to have its instance of the mutex re-initialized? I know child processes don't inherit threads, and a specific mutex initialization flag needs to be used if I want the two processes to truly share the mutex (can't recall what the flag is) or I have to make use of <code>mmap</code> IIRC. But is the mutex instance used by the child valid (ie: does <code>fork()</code> duplicate the internal values, but they aren't valid anymore, or is a new mutex initialized with the OS)? <strong>I don't want the child and parent process to share a mutex when a <code>fork()</code> occurs, but I want to make sure the client is using a valid mutex handle.</strong></p>
  <p>Note: I can guarantee that the mutex will <strong>not</strong> be locked when the <code>fork()</code> call is issued.</p>
  <p>Thank you.</p>