Linux进程间可重入信号量 [英] Linux inter-process reentrant semaphore

查看:638
本文介绍了Linux进程间可重入信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个Windows应用程序移植到Linux,我有一个同步问题。

I'm porting a Windows application to Linux and I have a synchronization problem.

在Windows中我使用系统级命名的mutex来同步访问共享内存块。

In Windows I'm using a system-level named mutex to sync access to a shared memory block.

如何在Linux中模拟?我创建了一个SystemV信号量,使用semget。问题是,它是不可重入,如果我已经拥有它会阻塞,不像在Windows上。我可以添加一个引用计数,但是我需要同步访问,这意味着另一个(这一次为当前进程只)互斥。

How do I emulate that in Linux? I've created a SystemV semaphore, using semget. The problem is that it is not reentrant, if I already hold it it will block, unlike on Windows. I could add a reference count to it, but then I would need to synchronize access to that, which means another (this time for the current process only) mutex.

Is有一个类提供了一个可重入的进程锁(也许在Boost)?

Is there a class somewhere which provides a reentrant interprocess lock (maybe in Boost)?

BTW,使用文件锁是不可接受的,因为它可能会太慢这两个进程之间的超低延迟通信)。

BTW, using a file lock is not acceptable since it will probably be too slow (I need ultra-low latency communication between the two processes).

推荐答案

您可以使用 interprocess), recursive pthread_mutex_t 。创建一个普通的pthread_mutex(存储在共享内存中)并使用 pthread_mutexattr_settype PTHREAD_MUTEX_RECURSIVE 标志设置其属性,然后调用 pthread_mutexattr_setpshared PTHREAD_MUTEX_SHARED 旗标。

You can just use a shared (interprocess), recursive pthread_mutex_t. Create a normal pthread_mutex (stored in shared memory) and set its attributes using pthread_mutexattr_settype with the PTHREAD_MUTEX_RECURSIVE flag, and then call pthread_mutexattr_setpshared with the PTHREAD_MUTEX_SHARED flag.

你是一个可重入的进程锁。

That will give you a reentrant, interprocess lock.

这篇关于Linux进程间可重入信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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