简体   繁体   English

Mac OS X 中的 sem_getvalue() 功能障碍 - C++

[英]sem_getvalue() dysfunctionality in Mac OS X - C++

I'm trying to implement synchronized use of a shared memory for bunch of threads in Mac OS X by semaphores.我正在尝试通过信号量为 Mac OS X 中的一堆线程实现共享内存的同步使用。

(I just overlook the fact that Mac users have got lots of issues with initializing a semaphore and destroying it...,which can be fixed by sem_open() and sem_unlink()):D (我只是忽略了一个事实,即 Mac 用户在初始化信号量和销毁它时遇到了很多问题......,可以通过 sem_open() 和 sem_unlink() 修复):D

But apparently for getting semaphore's current value there's nothing but sem_getvalue() which hasn't been implemented in mac os x.但显然为了获取信号量的当前值,除了 sem_getvalue() 没有在 mac os x 中实现。

Any suggestion for someone that doesn't have linux OS running and should upload his homework in hours??!:)对于没有运行 linux 操作系统并且应该在几小时内上传作业的人有什么建议吗??!:)

Thanks谢谢

I think you are asking, "how can I work around the absence of sem_getvalue() on OS X?"我想您是在问, “我如何解决 OS X 上没有sem_getvalue()问题?”

I can think of three approaches:我可以想到三种方法:

First (and best, in my opinion) redesign your program so that the current value of the semaphore is never needed.首先(在我看来最好)重新设计您的程序,以便永远不需要信号量的当前值。 After all, as the documentation warns , the value reported by sem_getvalue isn't necessarily accurate by the time it is received.毕竟, 正如文档所警告的那样sem_getvalue报告的值在收到时不一定准确。

Second, if necessary, wrap the POSIX semaphore functions and keep your own count.其次,如有必要,包装 POSIX 信号量函数并保持自己的计数。 Every sem_t can be adorned by a counter and a mutex protecting that counter.每个sem_t都可以装饰一个计数器和一个保护该计数器的互斥锁。 Your implementation probably will have (and probably should have!) the same caveat as sem_getvalue , that is, the count can't be trusted to be accurate once it is retrieved.您的实现可能会有(并且可能应该有!)与sem_getvalue相同的警告, sem_getvalue ,一旦检索到计数,就不能相信它是准确的。

Third, and least palatable in my opinion, switch to the older and more complicated SysV semaphore IPC interface.第三,在我看来最不愉快的是,切换到更老、更复杂的SysV 信号量 IPC接口。 That implements something analogous to sem_getvalue .这实现了类似于sem_getvalue东西。

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

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