0
У меня есть этот кусок кода:Доля потоковой мьютекс между процессами
typedef struct
{
// ... other fields ...
pthread_mutex_t Lock;
} TShared;
const int NPROCESSES=32;
pid_t pidprocesses[128];
for (int i=0;i<NPROCESSES;i++)
{
pidprocesses[i]=fork();
if (!pidprocesses[i])
{
sleep(5); // wait the main process
int shmid = shmget(1616,sizeof(TShared),0666);
if (shmid<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(&shm->Lock);
/* ... other code ... */
pthread_mutex_unlock(&shm->Lock);
}
exit(0);
}
}
// main process
int shmid = shmget(1616,sizeof(TShared),IPC_CREAT|0666);
if (shmid<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(&shm->Lock,NULL);
/* ... other code ... */
for (int i=0;i<NPROCESSES;i++) waitpid(pidprocesses[i],0,0);
Этот фрагмент кода создать 32 процессов, а затем использовать общую память с PTHREAD мьютекс синхронизировать их и в конце концов основной процесс ждать прекращения ребенка. Правильно ли это использование мьютекса pthread?