简体   繁体   English

Mac OS X中是否有等效的PTHREAD_MUTEX_ROBUST?

[英]Is there PTHREAD_MUTEX_ROBUST equivalent in Mac OS X?

I'm using a pthread_mutex_t with PTHREAD_PROCESS_SHARED on a shared memory to do synchronization between different processes. 我在共享内存上使用带有PTHREAD_PROCESS_SHAREDpthread_mutex_t在不同进程之间进行同步。

The mutex maybe deadlocked if a process exits but leaves the mutex locked. 如果进程退出但互斥锁处于锁定状态,则互斥锁可能会死锁。 There is a PTHREAD_MUTEX_ROBUST in POSIX standard. POSIX标准中有一个PTHREAD_MUTEX_ROBUST But it seems that Mac OS X does not support the PTHREAD_MUTEX_ROBUST . 但是,似乎Mac OS X不支持PTHREAD_MUTEX_ROBUST

Is there some kind of mutex on Mac OS X that can be used on a shared memory, and to be used to synchronize cross processes, and to be robust in case of a process die without unlock it? Mac OS X上是否有某种mutex可以在共享内存上使用,并且可以用于同步跨进程,并且在进程死机而没有解锁的情况下保持健壮性?

The robust stuff appeared in a later iteration of POSIX threads (SUSv7), not part of the the standard supported by Mac OS X (which is SUSv2). 强大的功能出现在POSIX线程(SUSv7)的更高版本中,而不是Mac OS X(SUSv2)支持的标准的一部分。

The Apple docs do not show a pthread_mutexattr_setrobust function (or its equivalent get ) and they state that they're based on SUSv2, so that explains why you don't have it. Apple文档未显示pthread_mutexattr_setrobust函数(或等效的get ),并且声明它们基于SUSv2,因此可以解释为什么没有它。

In terms of fixing the problem, you may consider something like the use of an atexit handler to free up whatever resources your exiting program may possess. 解决问题而言,您可以考虑使用atexit处理程序来释放现有程序可能拥有的任何资源。

Or another possibility is to monitor the deadlock externally and clean up if a problem is found. 或者另一种可能性是从外部监视死锁并在发现问题时进行清理。 For example, have a watchdog process with two threads along the following lines. 例如,有一个看门狗进程,该进程具有以下两行的两个线程。

thread1:
    set variables gloabalNum and localNum to zero
    start thread2
    while true:
        sleep 60 seconds
        if globalNum == localNum:
            exit while
        end if
        localNum = globalNum
    end while

    kill all processes using mutex
    remove shared memory
    exit process

thread2:
    while true:
        lock mutex
        unlock mutex
        increment globalNum
        sleep 5 second

The watchdog effectively locks and unlocks the mutex every five seconds, incrementing a variable each time. 看门狗每五秒钟有效地锁定和解锁互斥锁一次,每次都增加一个变量。 If, for some reason, you get deadlock, thread2 will halt and the variable will never be updated. 如果由于某种原因导致死锁, thread2将停止并且该变量将永远不会更新。

In the meantime, thread1 is checking to make sure thread2 is still running, by checking the variable against its local copy every minute. 同时, thread1正在检查以确保thread2仍在运行,方法是每分钟对变量的本地副本进行检查。 If it finds they're the same, it assumes thread2 has halted due to deadlock and it then cleans up everything by shutting down all processes using the mutex and destroying it (by deleting the shared memory). 如果发现它们相同,则假定thread2由于死锁而停止运行,然后通过使用互斥锁关闭所有进程并销毁它(通过删除共享内存)来清理所有内容。

The watchdog can then exit and presumably whatever code you already have for starting the entire application will kick in at some point. 然后看门狗可以退出,并且可能启动某个应用程序的任何代码都会在某个时候启动。 Or you can have the watchdog process raise an alert of some sort before exiting, to ensure the problem gets looked at. 或者,您可以让看门狗进程在退出之前发出某种警报,以确保问题得到解决。

The idea behind a watchdog process is to make it as simple as possible, hopefully to the point where it's provably correct (or at least more so than your errant program). 看门狗程序背后的想法是使它尽可能地简单,希望可以证明它是正确的(或者至少比错误的程序更正确)。

There are no doubt many other possibilities depending on your overall architecture. 毫无疑问,取决于您的整体体系结构,还有许多其他可能性。 I've just provided these few off the top of my head to give you something to think about. 我刚刚提供了这些建议,让您有所思考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM