msgqueue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msgqueue.h
  3. // Purpose: interface of wxMessageQueue<T>
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Error codes for wxMessageQueue<> operations.
  9. This enum contains the possible return value of wxMessageQueue<> methods.
  10. @since 2.9.0
  11. @category{threading}
  12. */
  13. enum wxMessageQueueError
  14. {
  15. /// Indicates that the operation completed successfully.
  16. wxMSGQUEUE_NO_ERROR = 0,
  17. /**
  18. Indicates that no messages were received before timeout expired.
  19. This return value is only used by wxMessageQueue<>::ReceiveTimeout().
  20. */
  21. wxMSGQUEUE_TIMEOUT,
  22. /// Some unexpected (and fatal) error has occurred.
  23. wxMSGQUEUE_MISC_ERROR
  24. };
  25. /**
  26. wxMessageQueue allows passing messages between threads.
  27. This class should be typically used to communicate between the main and worker
  28. threads. The main thread calls wxMessageQueue::Post and the worker thread
  29. calls wxMessageQueue::Receive.
  30. @tparam T
  31. For this class a message is an object of arbitrary type T.
  32. Notice that often there is a some special message indicating that the thread
  33. should terminate as there is no other way to gracefully shutdown a thread
  34. waiting on the message queue.
  35. @since 2.9.0
  36. @nolibrary
  37. @category{threading}
  38. @see wxThread
  39. */
  40. template <typename T>
  41. class wxMessageQueue<T>
  42. {
  43. public:
  44. /**
  45. Default and only constructor.
  46. Use wxMessageQueue::IsOk to check if the object was successfully initialized.
  47. */
  48. wxMessageQueue();
  49. /**
  50. Remove all messages from the queue.
  51. This method is meant to be called from the same thread(s) that call
  52. Post() to discard any still pending requests if they became
  53. unnecessary.
  54. @since 2.9.1
  55. */
  56. wxMessageQueueError Clear();
  57. /**
  58. Returns @true if the object had been initialized successfully, @false
  59. if an error occurred.
  60. */
  61. bool IsOk() const;
  62. /**
  63. Add a message to this queue and signal the threads waiting for messages
  64. (i.e. the threads which called wxMessageQueue::Receive or
  65. wxMessageQueue::ReceiveTimeout).
  66. This method is safe to call from multiple threads in parallel.
  67. */
  68. wxMessageQueueError Post(T const& msg);
  69. /**
  70. Block until a message becomes available in the queue.
  71. Waits indefinitely long or until an error occurs.
  72. The message is returned in @a msg.
  73. */
  74. wxMessageQueueError Receive(T& msg);
  75. /**
  76. Block until a message becomes available in the queue, but no more than
  77. @a timeout milliseconds has elapsed.
  78. If no message is available after @a timeout milliseconds then returns
  79. @b wxMSGQUEUE_TIMEOUT.
  80. If @a timeout is 0 then checks for any messages present in the queue
  81. and returns immediately without waiting.
  82. The message is returned in @a msg.
  83. */
  84. wxMessageQueueError ReceiveTimeout(long timeout, T& msg);
  85. };