I know how to use Unix semaphores in C. Before using them I must call a constructor-ish function named sem_init and after using them I have to call a destructor-like function named sem_destroy.
I know I can keep doing this in C++ because of its backwards compatibility with C, but does C++ have a real object-oriented way to use semaphores?
If you really insist on using POSIX semaphores and not Boost, you can of course wrap
sem_tin a class:Exercise for the reader: You may want to add exceptions instead of C-style error codes and perhaps other features. Also, this class should be noncopyable. The easiest way to achieve that is inheriting from
boost::noncopyable😉Edit: as @Ringding remarks, looping on
EINTRwould be a very wise thing to do.