间与互斥的pthreads [英] Interprocess mutex with pthreads

查看:88
本文介绍了间与互斥的pthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用将用于同步访问居住在黑/白两种不同进程共享内存一些变量互斥。我怎样才能做到这一点。 code样品进行,这将是非常美联社preciated。

I want to use a mutex which will be used to synchronize access to some variables residing in the memory shared b/w two different processes. How can I achieve that. Code sample to perform that will be very appreciated.

推荐答案

使用的 POSIX信号初始化为 1 来代替。使用 sem_init 的未命名信号或 sem_open 命名的。

Use a POSIX semaphore initialized to 1 instead. Use sem_init for unnamed semaphores or sem_open for named ones.

sem_t sem;

/* initialize using sem_init or sem_open */

sem_wait(&sem);
/* critical region */
sem_post(&sem);

这篇关于间与互斥的pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆