简体   繁体   English

pthread-win32扩展sem_post_multiple

[英]pthread-win32 extension sem_post_multiple

I am currently building a thin C++ wrapper around pthreads for in-house use. 我目前正在内部使用pthreads构建一个薄的C ++包装器。 Windows as well as QNX are targeted and fortunately the pthreads-win32 ports seems to work very well, while QNX is conformant to POSIX for our practical purposes. Windows和QNX都是针对性的,幸运的是pthreads-win32端口似乎运行良好,而QNX在我们的实际用途中符合POSIX。

Now, while implementing semaphores, I hit the function 现在,在实现信号量的同时,我点击了功能

sem_post_multiple(sem_t*, int)

which is apparently only available on pthreads-win32 but is missing from QNX. 它显然仅在pthreads-win32上可用,但QNX缺少。 As the name suggests the function is supposed to increment the semaphore by the count given as second argument. 顾名思义,该函数应该使信号量增加第二个参数给出的计数。 As far as I can tell the function is not part of neither POSIX 1b nor POSIX 1c. 据我所知,该功能既不是POSIX 1b也不是POSIX 1c的一部分。

Although there is currently no requirement for said function I am still wondering why pthreads-win32 provides the function and whether it might be of use. 尽管目前不需要上述功能,但我仍然想知道为什么pthreads-win32提供了该功能以及它是否有用。 I could try to mimic it for QNX using similar to the following: 我可以尝试使用类似于以下内容的QNX模拟它:

sem_post_multiple_qnx(sem_t* sem, int count)
{
   for(;count > 0; --count)
   {
       sem_post(sem);
   }
} 

What I am asking for are suggestion/advice on how to proceed. 我要的是关于如何进行的建议/建议。 If consensus suggests to do implement the function for QNX I would also appreciate comments on whether the suggested code snipped is a viable solution. 如果达成共识建议为QNX实现该功能,那么我也希望对所建议的代码是否被删除是可行的。

Thanks in advance. 提前致谢。

PS: I deliberately left out my fancy C++ class for clarity. PS:为了清楚起见,我故意省略了我喜欢的C ++类。 For all folks suggesting boost to the rescue: it is not an option in my current project due to management reasons. 对于所有建议加大救援力度的人:由于管理原因,在我当前的项目中这不是一个选择。

Your proposed implementation of sem_post_multiple doesn't play nicely with sem_getvalue , since sem_post_multiple is an atomic increase and therefore it's not possible for a "simultaneous" call to sem_getvalue to return any of the intermediate values. 您建议的sem_post_multiple实现与sem_getvalue不能很好地配合sem_getvalue ,因为sem_post_multiple是原子增加的,因此无法“同时”调用sem_getvalue来返回任何中间值。

Personally I'd want to leave them both out: trying to add fundamental synchronization operations to a system which lacks them is a mug's game, and your wrapper might soon cease to be "thin". 我个人想将它们都排除在外:尝试向缺少它们的系统中添加基本同步操作是一个杯具游戏,而您的包装器可能很快就会变得“薄”。 So don't get into it unless you have code that uses sem_post_multiple , that you absolutely have to port. 因此,除非您有使用sem_post_multiple代码,否则绝对不要移植它,而您绝对需要移植。

In any case semaphores are an optional extension in POSIX. 无论如何,信号量是POSIX中的可选扩展。 Eg OS X doesn't seem to implement them fully. 例如,OS X似乎并未完全实现它们。 So if you are concerned with portability, you'd have to provide wrappers of the functionalities you need, anyhow. 因此,如果您担心可移植性,则无论如何都必须提供所需功能的包装。

Your approach to emulate an atomic increment by iterated sem_post has certainly downsides. 您通过迭代sem_post模拟原子增量的方法肯定有缺点。

  • It might be performing badly, where usually sem_t are used in performance critical contexts. 在性能关键的上下文中通常使用sem_t ,它的性能可能很差。
  • This operation would not be atomic. 此操作不是原子操作。 So confusing things might happen before you finish the loop. 因此,在完成循环之前,可能会发生混乱的事情。

I would stick to the just necessary, strictly POSIX conforming. 我会坚持公正必要,严格遵循POSIX的原则。 Beware that sem_timedwait is yet another optional part of the semaphore option. 注意sem_timedwait是信号量选项的另一个可选部分。

sem_post_multiple() is a non-standard helper function introduced by the win32-pthreads maintainers. sem_post_multiple()是win32-pthreads维护者引入的非标准辅助函数。 Your implementation isn't the same as theirs because the multiple decrements aren't atomic. 您的实现与他们的实现不同,因为多个减量不是原子的。 Whether or not this is a problem depends on the intended use. 这是否有问题取决于预期的用途。 (Personally, I wouldn't try to implement this function unless/until the need arises.) (就个人而言,除非/直到有需要,否则我不会尝试实现此功能。)

This is an interesting question. 这是个有趣的问题。 +1. +1。

I agree with the current prevailing consensus here that it is probably not a good idea to implement that function. 我同意目前的普遍共识,即实施该功能可能不是一个好主意。 While your proposed implementation would probably be work just fine in most situations, there are definitely conditions in which the results could be dramatically different due to the non-atomicity. 尽管您建议的实现在大多数情况下可能会很好地工作,但由于非原子性,在某些情况下肯定会导致结果有很大不同。 The following is one (extremely) contrived situation: 以下是一种(极端)人为的情况:

  • Start thread A which calls sem_post_multiple( s, 10 ) 启动线程A,该线程调用sem_post_multiple(s,10)
  • Thread B waiting on s is released. 等待s的线程B被释放。 Thread B kills thread A. 线程B杀死线程A。

In the above unfriendly scenario, the atomic version would have incremented the semaphore by 10. With non-atomic version, it may only be incremented once. 在上述不友好的方案中,原子版本将信号量增加了10。对于非原子版本,信号量只能增加一次。 This example is certainly not likely in the real world. 这个例子在现实世界中肯定不太可能。 For example, the killing of a thread is almost always a bad idea not to mention the fact that it could leave the semaphore object in an invalid state. 例如,杀死线程几乎总是一个坏主意,更不用说它可能使信号量对象处于无效状态的事实。 The Win32 implementation could leave a mutex lock on the semaphore - see this for why . Win32实现可能会在信号量上保留互斥锁- 有关原因,请参见此内容

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

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