POCO C++库学习和分析 -- 通知和事件 (三)
4. 异步通知
4.1 NotificationQueue类
Poco中的异步通知是通过NotificationQueue类来实现的,同它功能类似还有类PriorityNotificationQueue和TimedNotificationQueue。不同的是PriorityNotificationQueue类中对消息分了优先级,对优先级高的消息优先处理;而TimedNotificationQueue对消息给了时间戳,时间戳早的优先处理,而和其压入队列的时间无关。所以接下来我们主要关注NotificationQueue的实现。事实上NotificationQueue是个非常有趣的类。让我们来看一下它的头文件:
class Foundation_API NotificationQueue
/// A NotificationQueue object provides a way to implement asynchronous
/// notifications. This is especially useful for sending notifications
/// from one thread to another, for example from a background thread to
/// the main (user interface) thread.
///
/// The NotificationQueue can also be used to distribute work from
/// a controlling thread to one or more worker threads. Each worker thread
/// repeatedly calls waitDequeueNotification() and processes the
/// returned notification. Special care must be taken when shutting
/// down a queue with worker threads waiting for notifications.
/// The recommended sequence to shut down and destroy the queue is to
/// 1. set a termination flag for every worker thread
/// 2. call the wakeUpAll() method
/// 3. join each worker thread
/// 4. destroy the notification queue.
{
public:
NotificationQueue();
/// Creates the NotificationQueue.
~NotificationQueue();
/// Destroys the NotificationQueue.
void enqueueNotification(Notification::Ptr pNotification);
/// Enqueues the given notification by adding it to
/// the end of the queue (FIFO).
/// The queue takes ownership of the notification, thus
/// a call like
/// notificationQueue.e