title: Share pthread mutex between processes
body: <p>I have this piece of code:</p>
  
  <pre><code>typedef struct
  {
      // ... other fields ...   
      pthread_mutex_t Lock;
  } TShared;
  
  
  const int NPROCESSES=32;   
  pid_t pidprocesses[128];
  for (int i=0;i&lt;NPROCESSES;i++)
  {
     pidprocesses[i]=fork();
     if (!pidprocesses[i])
     {  
        sleep(5); // wait the main process
        int shmid = shmget(1616,sizeof(TShared),0666);
        if (shmid&lt;0)
        {
           printf("Error shmget!\n");
           exit(0);
        }
        TShared *shm = (TShared *) shmat(shmid,NULL,0);
        if (shm==-1)
        {
           printf("Error shmat!\n");
           exit(0);
        }
        bool cond=true;       
        while(cond)
        {
           pthread_mutex_lock(&amp;shm-&gt;Lock);
           /* ... other code ... */         
           pthread_mutex_unlock(&amp;shm-&gt;Lock);
        }
        exit(0);
     }
  }
  // main process
  int shmid = shmget(1616,sizeof(TShared),IPC_CREAT|0666);
  if (shmid&lt;0)
  {
     printf("Error shmget!\n");
     exit(0);
  }
  TShared *shm = (TShared *) shmat(shmid,NULL,0);
  if (shm==-1)
  {
     printf("Error shmat!\n");
     exit(0);
  }
  pthread_mutex_init(&amp;shm-&gt;Lock,NULL);
  /* ... other code ... */
  for (int i=0;i&lt;NPROCESSES;i++) waitpid(pidprocesses[i],0,0);
  </code></pre>
  
  <p>This piece of code create 32 processes and then use shared memory with a pthread mutex to synchronize them and in the end the main process wait for the terminations of the childs.
  Is this the correct way use a pthread mutex?</p>