2
// NotificationQueue.cpp
4
// $Id: //poco/1.2/Foundation/samples/NotificationQueue/src/NotificationQueue.cpp#1 $
6
// This sample demonstrates the NotificationQueue, ThreadPool,
7
// FastMutex and ScopedLock classes.
9
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
12
// Permission is hereby granted, free of charge, to any person or organization
13
// obtaining a copy of the software and accompanying documentation covered by
14
// this license (the "Software") to use, reproduce, display, distribute,
15
// execute, and transmit the Software, and to prepare derivative works of the
16
// Software, and to permit third-parties to whom the Software is furnished to
17
// do so, all subject to the following:
19
// The copyright notices in the Software and this entire statement, including
20
// the above license grant, this restriction and the following disclaimer,
21
// must be included in all copies of the Software, in whole or in part, and
22
// all derivative works of the Software, unless such copies or derivative
23
// works are solely in the form of machine-executable object code generated by
24
// a source language processor.
26
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32
// DEALINGS IN THE SOFTWARE.
36
#include "Poco/Notification.h"
37
#include "Poco/NotificationQueue.h"
38
#include "Poco/ThreadPool.h"
39
#include "Poco/Thread.h"
40
#include "Poco/Runnable.h"
41
#include "Poco/Mutex.h"
42
#include "Poco/Random.h"
43
#include "Poco/AutoPtr.h"
47
using Poco::Notification;
48
using Poco::NotificationQueue;
49
using Poco::ThreadPool;
52
using Poco::FastMutex;
57
class WorkNotification: public Notification
58
// The notification sent to worker threads.
61
WorkNotification(int data):
76
class Worker: public Runnable
77
// A worker thread that gets work items
78
// from a NotificationQueue.
81
Worker(const std::string& name, NotificationQueue& queue):
92
AutoPtr<Notification> pNf(_queue.waitDequeueNotification());
95
WorkNotification* pWorkNf = dynamic_cast<WorkNotification*>(pNf.get());
98
FastMutex::ScopedLock lock(_mutex);
100
std::cout << _name << " got work notification " << pWorkNf->data() << std::endl;
102
Thread::sleep(rnd.next(200));
111
NotificationQueue& _queue;
112
static FastMutex _mutex;
116
FastMutex Worker::_mutex;
119
int main(int argc, char** argv)
121
NotificationQueue queue;
123
// create some worker threads
124
Worker worker1("Worker 1", queue);
125
Worker worker2("Worker 2", queue);
126
Worker worker3("Worker 3", queue);
128
// start worker threads
129
ThreadPool::defaultPool().start(worker1);
130
ThreadPool::defaultPool().start(worker2);
131
ThreadPool::defaultPool().start(worker3);
133
// distribute some work
134
for (int i = 0; i < 50; ++i)
136
queue.enqueueNotification(new WorkNotification(i));
139
// wait until queue is empty and all threads are
140
// waiting for new work.
141
while (!queue.empty()) Thread::sleep(200);
144
// stop all worker threads
146
ThreadPool::defaultPool().joinAll();