Linux的进程间折返信号 [英] Linux inter-process reentrant semaphore
问题描述
我移植一个Windows应用程序到Linux和我有一个同步问题。
I'm porting a Windows application to Linux and I have a synchronization problem.
在Windows中,我使用一个名为互斥的系统级同步访问共享内存块。
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 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).
推荐答案
您可以只使用一个的共享的(进程间)的递归的 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屋!