相当于 Mac OS X 中 Windows 命名的互斥锁?
Equivalent of Windows's named mutex in Mac OS X?
目前我正在用c++将一个软件从Windows移植到Mac OS X。
在Windows中,在全局命名互斥中有一个废弃状态,这意味着互斥的当前所有者进程在不释放互斥的情况下已经消失。(很可能是应用程序崩溃造成的)
由于存在放弃状态,尝试锁定被放弃的互斥锁不会导致死锁。
如果没有放弃状态,它将永远等待一个不属于任何人的互斥锁。
还有另一种方法,如果在一定时间内无法获得互斥锁,则使用timeout来假设互斥锁被放弃,但与放弃互斥锁的方法相比,这不是一个完美的解决方案。在最坏的情况下,两个进程可能意外地访问被互斥锁的对象。
在Mac OS X/Linux中是否有互斥支持放弃状态?
我研究了boost库,boost库有一个命名互斥锁,但这个互斥锁是基于共享文件的,所以它没有放弃状态。
请给我一些建议
也许有点晚了,但是你可以使用pthread_mutexattr_t
来设置你的互斥属性在pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
API之间共享。这个互斥锁的值需要在进程之间共享,可以将它存储在指定的共享内存中。
int key = ftok(NAMED_MEMORY, ID_TAG);
if (-1 == key)
{
printf("Unable to name shared memoryn");
exit(1);
}
// Create the segment exclusively (if the segment already exists then a combination of IPC_CREAT | IPC_EXCL returns an error EEXIST)
int m_iShmid = shmget(key, TOTAL_SIZE, READ_WRITE_PERMISSIONS | IPC_CREAT | IPC_EXCL);
if (m_iShmid < 0)
{
if (EEXIST == errno)
{
// if the shared memory already exists we only fetch the id to that memory
m_iShmid = shmget(key, TOTAL_SIZE, READ_WRITE_PERMISSIONS);
}
if (m_iShmid < 0)
{
printf("Unable to create shared memory - %sn",strerror(errno));
exit(1);
}
else
printf("Attached to the existing shared memoryn");
}
else
printf("Created new shared memoryn");
// Now we attach the segment to our data space.
mutex = reinterpret_cast<pthread_mutex_t*>(shmat(m_iShmid, NULL, 0));
if (reinterpret_cast<pthread_mutex_t*>(-1) == mutex)
{
printf("Unable to attach shared memory to the process - %sn",strerror(errno));
exit(1);
}
// Now we can set this mutex to be shared between processes
pthread_mutex_t* mutex;
pthread_mutexattr_t mutexAttr;
ret = pthread_mutexattr_init(&mutexAttr);
if(ret != 0)
{
printf("pthread_mutexattr_init failed - err=%dn",ret);
exit(1);
}
ret = pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
if(ret != 0)
{
printf("pthread_mutexattr_setpshared failed - err=%dn",ret);
exit(1);
}
ret = pthread_mutexattr_setrobust_np(&mutexAttr, PTHREAD_MUTEX_ROBUST_NP);
if(ret != 0)
{
printf("pthread_mutexattr_setrobust_np failed - err=%dn",ret);
exit(1);
}
ret = pthread_mutex_init(mutex, &mutexAttr);
if(ret != 0)
{
printf("pthread_mutex_init failed - err=%dn",ret);
exit(1);
}
// ------ Use the mutex from here on between processes
考虑到这里唯一的答案是使用古老的ftok()/shmget()方法来获取共享内存,我将指向我开发和维护的一个库:
https://github.com/cubiclesoft/cross-platform-cpp具体来说,你需要的是'sync/sync_mutex '。*和sync/sync_util。*’文件。在Windows、Mac、Linux以及其他一些类型的操作系统中,使用现代POSIX pthread和POSIX共享内存,在* nix风格的操作系统上使用单个Sync::Mutex类。这段代码还处理了一些场景,比如只让一个线程创建和初始化对象,而其他线程要等到对象完全初始化后才能继续。
库的Sync部分过去使用POSIX信号量,但我发现在某些操作系统上,当共享内存和pthread在相同的操作系统上共享更广泛的支持时,这些信号量是相当坏的。
就放弃状态而言,操作系统本身必须获得同步对象的所有权来处理特定的场景。也就是说,进程退出,所有获得的同步对象被标记为放弃。Windows内核通常在进程退出时处理同步对象的清理。其他操作系统内核不会/不能这样做。一种可能的选择是为其他操作系统编写系统服务或驱动程序,其唯一目的是处理废弃状态并清理和重新初始化对象。当然,如果你在应用程序中找到了一个处理废弃对象的好方法,请告诉我/提交一个补丁/等等。
- libprotobuf 检查在 Mac OS 上执行程序时失败
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何在 MAC OS c/c++ 中阻止 USB 存储设备
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- const 如何防止写入特定内存 [Clang, Mac OS]
- 如何在 Mac OS 上安装 boost-mpi 及其对 clang 的依赖关系?
- 在 Mac OS 中将 QT 与 CMAKE 集成
- Homebrew OS X OpenCV - 在另一台Mac上运行时寻找dylib
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- VK_ERROR_INCOMPATIBLE_DRIVER with Mac OS and Vulkan MoltenVK
- 我无法在C++(Clion,Mac OS)中运行Gurobi
- 如何在Mac OS的发布版本中包含Qt库?
- 如何将C++项目从 32 位迁移到 64 位以支持 MAC OS 10.14(莫哈韦)?
- 在 Mac OS x 中制造期间出现错误未知类型名称"constexpr"
- 无法在 Mac OS X 上链接静态资源库
- 在Mac OS X Mountain Lion(或更高版本)和Linux上使用X11/Xlib.h
- Mac OS c++ Link OpenCV and libtiff
- 使用 c++ 在 Mac OS X 上启动"new email"窗口
- 在 Mac OS 上运行 x11
- 为什么 Mac OS 指定的 TCP 端口不侦听?