source: XMLIO_V2/external/src/POCO/Foundation.save/Poco/ThreadPool.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1//
2// ThreadPool.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/ThreadPool.h#3 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  ThreadPool
9//
10// Definition of the ThreadPool class.
11//
12// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38
39#ifndef Foundation_ThreadPool_INCLUDED
40#define Foundation_ThreadPool_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/Thread.h"
45#include "Poco/Mutex.h"
46#include <vector>
47
48
49namespace Poco {
50
51
52class Runnable;
53class PooledThread;
54
55
56class Foundation_API ThreadPool
57        /// A thread pool always keeps a number of threads running, ready
58        /// to accept work.
59        /// Creating and starting a threads can impose a significant runtime
60        /// overhead to an application. A thread pool helps to improve
61        /// the performance of an application by reducing the number
62        /// of threads that have to be created (and destroyed again).
63        /// Threads in a thread pool are re-used once they become
64        /// available again.
65        /// The thread pool always keeps a minimum number of threads
66        /// running. If the demans for threads increases, additional
67        /// threads are created. Once the demand for threads sinks
68        /// again, no-longer used threads are stopped and removed
69        /// from the pool.
70{
71public:
72        ThreadPool(int minCapacity = 2,
73                int maxCapacity = 16,
74                int idleTime = 60,
75                int stackSize = POCO_THREAD_STACK_SIZE);
76        ThreadPool(const std::string& name,
77                int minCapacity = 2,
78                int maxCapacity = 16,
79                int idleTime = 60,
80                int stackSize = POCO_THREAD_STACK_SIZE);
81                /// Creates a thread pool with minCapacity threads.
82                /// If required, up to maxCapacity threads are created
83                /// a NoThreadAvailableException exception is thrown.
84                /// If a thread is running idle for more than idleTime seconds,
85                /// and more than minCapacity threads are running, the thread
86                /// is killed. Threads are created with given stack size.
87
88        ~ThreadPool();
89                /// Currently running threads will remain active
90                /// until they complete.
91       
92        void addCapacity(int n);
93                /// Increases (or decreases, if n is negative)
94                /// the maximum number of threads.
95
96        int capacity() const;
97                /// Returns the maximum capacity of threads.
98
99        void setStackSize(int stackSize);
100                /// Sets the stack size for threads.
101                /// New stack size applies only for newly created threads.
102
103        int getStackSize() const;
104                /// Returns the stack size used to create new threads.
105
106        int used() const;
107                /// Returns the number of currently used threads.
108
109        int allocated() const;
110                /// Returns the number of currently allocated threads.
111
112        int available() const;
113                /// Returns the number available threads.
114
115        void start(Runnable& target);
116                /// Obtains a thread and starts the target.
117                /// Throws a NoThreadAvailableException if no more
118                /// threads are available.
119
120        void start(Runnable& target, const std::string& name);
121                /// Obtains a thread and starts the target.
122                /// Assigns the given name to the thread.
123                /// Throws a NoThreadAvailableException if no more
124                /// threads are available.
125
126        void startWithPriority(Thread::Priority priority, Runnable& target);
127                /// Obtains a thread, adjusts the thread's priority, and starts the target.
128                /// Throws a NoThreadAvailableException if no more
129                /// threads are available.
130
131        void startWithPriority(Thread::Priority priority, Runnable& target, const std::string& name);
132                /// Obtains a thread, adjusts the thread's priority, and starts the target.
133                /// Assigns the given name to the thread.
134                /// Throws a NoThreadAvailableException if no more
135                /// threads are available.
136
137        void stopAll();
138                /// Stops all running threads.
139                /// Will also delete all thread objects.
140                /// If used, this method should be the last action before
141                /// the thread pool is deleted.
142
143        void joinAll();
144                /// Waits for all threads to complete.
145
146        void collect();
147                /// Stops and removes no longer used threads from the
148                /// thread pool. Can be called at various times in an
149                /// application's life time to help the thread pool
150                /// manage its threads. Calling this method is optional,
151                /// as the thread pool is also implicitly managed in
152                /// calls to start(), addCapacity() and joinAll().
153
154        static ThreadPool& defaultPool();
155                /// Returns a reference to the default
156                /// thread pool.
157
158protected:
159        PooledThread* getThread();
160        PooledThread* createThread();
161
162        void housekeep();
163
164private:
165        ThreadPool(const ThreadPool& pool);
166        ThreadPool& operator = (const ThreadPool& pool);
167
168        typedef std::vector<PooledThread*> ThreadVec;
169
170        std::string _name;
171        int _minCapacity;
172        int _maxCapacity;
173        int _idleTime;
174        int _serial;
175        int _age;
176        int _stackSize;
177        ThreadVec _threads;
178        mutable FastMutex _mutex;
179};
180
181
182//
183// inlines
184//
185inline void ThreadPool::setStackSize(int stackSize)
186{
187        _stackSize = stackSize;
188}
189
190
191inline int ThreadPool::getStackSize() const
192{
193        return _stackSize;
194}
195
196
197} // namespace Poco
198
199
200#endif // Foundation_ThreadPool_INCLUDED
Note: See TracBrowser for help on using the repository browser.