SynchronizedObject.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // SynchronizedObject.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/SynchronizedObject.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: SynchronizedObject
  9. //
  10. // Definition of the SynchronizedObject 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_SynchronizedObject_INCLUDED
  18. #define Foundation_SynchronizedObject_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Mutex.h"
  21. #include "Poco/Event.h"
  22. namespace Poco {
  23. class Foundation_API SynchronizedObject
  24. /// This class aggregates a Mutex and an Event
  25. /// and can act as a base class for all objects
  26. /// requiring synchronization in a multithreaded
  27. /// scenario.
  28. {
  29. public:
  30. typedef Poco::ScopedLock<SynchronizedObject> ScopedLock;
  31. SynchronizedObject();
  32. /// Creates the object.
  33. virtual ~SynchronizedObject();
  34. /// Destroys the object.
  35. void lock() const;
  36. /// Locks the object. Blocks if the object
  37. /// is locked by another thread.
  38. bool tryLock() const;
  39. /// Tries to lock the object. Returns false immediately
  40. /// if the object is already locked by another thread
  41. /// Returns true if the object was successfully locked.
  42. void unlock() const;
  43. /// Unlocks the object so that it can be locked by
  44. /// other threads.
  45. void notify() const;
  46. /// Signals the object.
  47. /// Exactly only one thread waiting for the object
  48. /// can resume execution.
  49. void wait() const;
  50. /// Waits for the object to become signalled.
  51. void wait(long milliseconds) const;
  52. /// Waits for the object to become signalled.
  53. /// Throws a TimeoutException if the object
  54. /// does not become signalled within the specified
  55. /// time interval.
  56. bool tryWait(long milliseconds) const;
  57. /// Waits for the object to become signalled.
  58. /// Returns true if the object
  59. /// became signalled within the specified
  60. /// time interval, false otherwise.
  61. private:
  62. mutable Mutex _mutex;
  63. mutable Event _event;
  64. };
  65. //
  66. // inlines
  67. //
  68. inline void SynchronizedObject::lock() const
  69. {
  70. _mutex.lock();
  71. }
  72. inline bool SynchronizedObject::tryLock() const
  73. {
  74. return _mutex.tryLock();
  75. }
  76. inline void SynchronizedObject::unlock() const
  77. {
  78. _mutex.unlock();
  79. }
  80. inline void SynchronizedObject::notify() const
  81. {
  82. _event.set();
  83. }
  84. inline void SynchronizedObject::wait() const
  85. {
  86. _event.wait();
  87. }
  88. inline void SynchronizedObject::wait(long milliseconds) const
  89. {
  90. _event.wait(milliseconds);
  91. }
  92. inline bool SynchronizedObject::tryWait(long milliseconds) const
  93. {
  94. return _event.tryWait(milliseconds);
  95. }
  96. } // namespace Poco
  97. #endif // Foundation_SynchronizedObject_INCLUDED