diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-11-17 22:21:09 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-11-17 22:21:09 +0000 |
commit | 22fe281b639187960a9ca92c58676ad55bcc0769 (patch) | |
tree | 06f9a096cd1421e0b301e1ca89dd88dfd76aaad6 /libadhocutil/semaphore.h | |
parent | Improve core with traits and decltype for working with functions that return ... (diff) | |
download | libadhocutil-22fe281b639187960a9ca92c58676ad55bcc0769.tar.bz2 libadhocutil-22fe281b639187960a9ca92c58676ad55bcc0769.tar.xz libadhocutil-22fe281b639187960a9ca92c58676ad55bcc0769.zip |
Add a portable semaphore class with timeout support
Diffstat (limited to 'libadhocutil/semaphore.h')
-rw-r--r-- | libadhocutil/semaphore.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libadhocutil/semaphore.h b/libadhocutil/semaphore.h new file mode 100644 index 0000000..de10333 --- /dev/null +++ b/libadhocutil/semaphore.h @@ -0,0 +1,29 @@ +#ifndef ADHOCUTIL_SEMAPHORE_H +#define ADHOCUTIL_SEMAPHORE_H + +// Borrowed from StackOverflow +// http://stackoverflow.com/questions/4792449/c0x-has-no-semaphores-how-to-synchronize-threads + +#include <boost/thread/condition.hpp> +#include <boost/thread/mutex.hpp> +#include "visibility.h" + +namespace AdHoc { + /// A portable semaphore with timeout support + class DLL_PUBLIC Semaphore { + public: + Semaphore(unsigned int initial = 0); + + void notify(); + void wait(); + bool wait(unsigned int); + + private: + boost::mutex mutex; + boost::condition_variable condition; + unsigned long count; + }; +} + +#endif + |