Several questions on this site are related to the lack of a semaphore object in multi-threaded support introduced in C ++ 11. Many have suggested implementing semaphores using mutexes or variable conditions, or a combination of both .
However, none of these approaches allows you to increase and decrease the semaphore, ensuring that the calling thread is not blocked, since it is usually necessary to obtain a lock before writing the semaphore value. The POSIX semaphore, for example, has the functions sem_post() and sem_trywait() , both of which are not blocked.
Is it possible to implement a non-blocking semaphore with support for C ++ 11 multithreading? Or should I definitely use an OS-specific library for this? If so, why does C ++ 11 not include a semaphore object?
A similar question was not received after 3 years. (Note: I believe that the question I ask is much wider, although, of course, there are other uses for the non-blocking semaphore object other than the producer / consumer. If, despite this, someone believes that my question is a duplicate, then please tell me how I can pay attention to the old question, since it still remains open.)
source share