Pthread-win32 sem_post_multiple extension

I am currently creating a thin C ++ wrapper around pthreads for internal use. Windows and QNX are oriented, and fortunately, the pthreads-win32 ports work very well, and QNX is POSIX compliant for our practical purposes.

Now, executing the semaphores, I click on the function

sem_post_multiple(sem_t*, int) 

which, apparently, is only available on pthreads-win32, but not in QNX. As the name implies, the function should increase the semaphore by the account specified as the second argument. As far as I can tell, the function is not part of either POSIX 1b or POSIX 1c.

Although there is currently no requirement for this function, I'm still wondering why pthreads-win32 provides this function and whether it can be useful. I could try to simulate it for QNX using something like the following:

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

What I am asking is a suggestion / advice on how to proceed. If consensus is to implement a function for QNX, I would also like to comment on whether the proposed code is shortened - this is a viable solution.

Thanks in advance.

PS: I specifically missed my C ++ class for clarity. For all people who suggest speeding up salvation: this is not an option in my current project due to managerial considerations.

+4
source share
4 answers

In any case, semaphores are an optional extension to POSIX. For example, OS X does not seem to fully implement them. Therefore, if you are interested in portability, you will have to provide the shell of functionality that you need.

Your approach to emulating atomic incrementation using the sem_post iteration sem_post has flaws.

  • This can be bad where sem_t is usually used in critical performance contexts.
  • This operation will not be atomic. So confusing things can happen before you finish the cycle.

I would adhere to only the necessary, strict POSIX compliance. Remember that sem_timedwait is another optional part of the semaphore option.

+1
source

Your proposed sem_post_multiple implementation sem_post_multiple not play well with sem_getvalue , since sem_post_multiple is an atomic increase, and therefore it is not possible to call sem_getvalue return any of the intermediate values ​​at the same time.

Personally, I would like to leave them both: an attempt to add the basic synchronization operations to a system in which they are not present is a game with a mug, and your wrapper may soon cease to be β€œthin”. So do not go into it unless you have code that uses sem_post_multiple , which you absolutely need for the port.

+1
source

sem_post_multiple () is a non-standard helper function introduced by third-party win32-pthreads developers. Your implementation is not the same as theirs, because multiple decrements are not atomic. Regardless of whether this is a problem, depends on the intended use. (Personally, I would not try to implement this function if / until such a need arises.)

0
source

This is an interesting question. +1

I agree with the current prevailing consensus here, which is probably not a good idea to implement this feature. Although your proposed implementation is likely to be very good in most situations, there are certain conditions under which the results can be significantly different due to non-atomicity. The following is one (extremely) contrived situation:

  • Run thread A, which calls sem_post_multiple (s, 10)
  • Dispatched stream B, waiting on s. Thread B kills thread A.

In the above unfriendly scenario, the atomic version would increase the semaphore by 10. With the non-atomic version, it can only be increased once. This example, of course, is hardly possible in the real world. For example, killing a stream is almost always a bad idea, not to mention that it can leave the semaphore object in an unacceptable state. A Win32 implementation may leave a mutex lock on the semaphore - see this for what .

0
source

All Articles