~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/samples/NotificationQueue/src/NotificationQueue.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// NotificationQueue.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/samples/NotificationQueue/src/NotificationQueue.cpp#1 $
 
5
//
 
6
// This sample demonstrates the NotificationQueue, ThreadPool,
 
7
// FastMutex and ScopedLock classes.
 
8
//
 
9
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
10
// and Contributors.
 
11
//
 
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:
 
18
// 
 
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.
 
25
// 
 
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.
 
33
//
 
34
 
 
35
 
 
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"
 
44
#include <iostream>
 
45
 
 
46
 
 
47
using Poco::Notification;
 
48
using Poco::NotificationQueue;
 
49
using Poco::ThreadPool;
 
50
using Poco::Thread;
 
51
using Poco::Runnable;
 
52
using Poco::FastMutex;
 
53
using Poco::Random;
 
54
using Poco::AutoPtr;
 
55
 
 
56
 
 
57
class WorkNotification: public Notification
 
58
        // The notification sent to worker threads.
 
59
{
 
60
public:
 
61
        WorkNotification(int data):
 
62
                _data(data)
 
63
        {
 
64
        }
 
65
        
 
66
        int data() const
 
67
        {
 
68
                return _data;
 
69
        }
 
70
 
 
71
private:
 
72
        int _data;
 
73
};
 
74
 
 
75
 
 
76
class Worker: public Runnable
 
77
        // A worker thread that gets work items
 
78
        // from a NotificationQueue.
 
79
{
 
80
public:
 
81
        Worker(const std::string& name, NotificationQueue& queue):
 
82
                _name(name),
 
83
                _queue(queue)
 
84
        {
 
85
        }
 
86
        
 
87
        void run()
 
88
        {
 
89
                Random rnd;
 
90
                for (;;)
 
91
                {
 
92
                        AutoPtr<Notification> pNf(_queue.waitDequeueNotification());
 
93
                        if (pNf)
 
94
                        {
 
95
                                WorkNotification* pWorkNf = dynamic_cast<WorkNotification*>(pNf.get());
 
96
                                if (pWorkNf)
 
97
                                {
 
98
                                        FastMutex::ScopedLock lock(_mutex);
 
99
                                        
 
100
                                        std::cout << _name << " got work notification " << pWorkNf->data() << std::endl;
 
101
                                        
 
102
                                        Thread::sleep(rnd.next(200));
 
103
                                }
 
104
                        }
 
105
                        else break;
 
106
                }
 
107
        }
 
108
        
 
109
private:
 
110
        std::string        _name;
 
111
        NotificationQueue& _queue;
 
112
        static FastMutex   _mutex;
 
113
};
 
114
 
 
115
 
 
116
FastMutex Worker::_mutex;
 
117
 
 
118
 
 
119
int main(int argc, char** argv)
 
120
{
 
121
        NotificationQueue queue;
 
122
        
 
123
        // create some worker threads
 
124
        Worker worker1("Worker 1", queue);
 
125
        Worker worker2("Worker 2", queue);
 
126
        Worker worker3("Worker 3", queue);
 
127
 
 
128
        // start worker threads
 
129
        ThreadPool::defaultPool().start(worker1);
 
130
        ThreadPool::defaultPool().start(worker2);
 
131
        ThreadPool::defaultPool().start(worker3);
 
132
 
 
133
        // distribute some work
 
134
        for (int i = 0; i < 50; ++i)
 
135
        {
 
136
                queue.enqueueNotification(new WorkNotification(i));
 
137
        }
 
138
        
 
139
        // wait until queue is empty and all threads are 
 
140
        // waiting for new work.
 
141
        while (!queue.empty()) Thread::sleep(200);
 
142
        Thread::sleep(500);
 
143
        
 
144
        // stop all worker threads
 
145
        queue.wakeUpAll();
 
146
        ThreadPool::defaultPool().joinAll();
 
147
        
 
148
        return 0;
 
149
}