Event.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Event.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Event.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Event
  9. //
  10. // Definition of the Event class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Event_INCLUDED
  18. #define Foundation_Event_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #if defined(POCO_OS_FAMILY_WINDOWS)
  22. #include "Poco/Event_WIN32.h"
  23. #elif defined(POCO_VXWORKS)
  24. #include "Poco/Event_VX.h"
  25. #else
  26. #include "Poco/Event_POSIX.h"
  27. #endif
  28. namespace Poco {
  29. class Foundation_API Event: private EventImpl
  30. /// An Event is a synchronization object that
  31. /// allows one thread to signal one or more
  32. /// other threads that a certain event
  33. /// has happened.
  34. /// Usually, one thread signals an event,
  35. /// while one or more other threads wait
  36. /// for an event to become signalled.
  37. {
  38. public:
  39. Event(bool autoReset = true);
  40. /// Creates the event. If autoReset is true,
  41. /// the event is automatically reset after
  42. /// a wait() successfully returns.
  43. ~Event();
  44. /// Destroys the event.
  45. void set();
  46. /// Signals the event. If autoReset is true,
  47. /// only one thread waiting for the event
  48. /// can resume execution.
  49. /// If autoReset is false, all waiting threads
  50. /// can resume execution.
  51. void wait();
  52. /// Waits for the event to become signalled.
  53. void wait(long milliseconds);
  54. /// Waits for the event to become signalled.
  55. /// Throws a TimeoutException if the event
  56. /// does not become signalled within the specified
  57. /// time interval.
  58. bool tryWait(long milliseconds);
  59. /// Waits for the event to become signalled.
  60. /// Returns true if the event
  61. /// became signalled within the specified
  62. /// time interval, false otherwise.
  63. void reset();
  64. /// Resets the event to unsignalled state.
  65. private:
  66. Event(const Event&);
  67. Event& operator = (const Event&);
  68. };
  69. //
  70. // inlines
  71. //
  72. inline void Event::set()
  73. {
  74. setImpl();
  75. }
  76. inline void Event::wait()
  77. {
  78. waitImpl();
  79. }
  80. inline void Event::wait(long milliseconds)
  81. {
  82. if (!waitImpl(milliseconds))
  83. throw TimeoutException();
  84. }
  85. inline bool Event::tryWait(long milliseconds)
  86. {
  87. return waitImpl(milliseconds);
  88. }
  89. inline void Event::reset()
  90. {
  91. resetImpl();
  92. }
  93. } // namespace Poco
  94. #endif // Foundation_Event_INCLUDED