File: ActiveThreadPool.h

package info (click to toggle)
poco 1.14.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 56,460 kB
  • sloc: cpp: 340,542; ansic: 245,601; makefile: 1,742; yacc: 1,005; sh: 698; sql: 312; lex: 282; xml: 128; perl: 29; python: 24
file content (98 lines) | stat: -rw-r--r-- 2,818 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// ActiveThreadPool.h
//
// Library: Foundation
// Package: Threading
// Module:  ActiveThreadPool
//
// Definition of the ActiveThreadPool class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_ActiveThreadPool_INCLUDED
#define Foundation_ActiveThreadPool_INCLUDED


#include "Poco/Foundation.h"
#include "Poco/Thread.h"
#include "Poco/Environment.h"
#include <memory>


namespace Poco {


class Runnable;
class ActiveThreadPoolPrivate;


class Foundation_API ActiveThreadPool
	/// A thread pool manages and recycles individual Poco::Thread objects
	/// to help reduce thread creation costs in programs that use threads.
	/// 
	/// The thread pool supports a task queue.
	/// When there are no idle threads, tasks are placed in the task queue to wait for execution.
	/// Use case for this pool is running many (more than os-max-thread-count) short live tasks
{
public:
	ActiveThreadPool(int capacity = static_cast<int>(Environment::processorCount()) + 1,
		int stackSize = POCO_THREAD_STACK_SIZE);
		/// Creates a thread pool with a maximum thread count of capacity.
		/// Threads are created with given stack size.

	ActiveThreadPool(const std::string& name,
		int capacity = static_cast<int>(Environment::processorCount()) + 1,
		int stackSize = POCO_THREAD_STACK_SIZE);
		/// Creates a thread pool with the given name and a maximum thread count of capacity.
		/// Threads are created with given stack size.

	~ActiveThreadPool();
		/// Currently running threads will remain active
		/// until they complete.

	int capacity() const;
		/// Returns the capacity of threads.

	int getStackSize() const;
		/// Returns the stack size used to create new threads.

	int expiryTimeout() const;
		/// Returns the thread expiry timeout value in milliseconds.
		/// The default expiryTimeout is 30000 milliseconds (30 seconds).

	void setExpiryTimeout(int expiryTimeout);
		/// Set the thread expiry timeout value in milliseconds.
		/// The default expiryTimeout is 30000 milliseconds (30 seconds).

	void start(Runnable& target, int priority = 0);
		/// Obtains a thread and starts the target.

	void joinAll();
		/// Waits for all threads to exit and removes all threads from the thread pool.

	const std::string& name() const;
		/// Returns the name of the thread pool,
		/// or an empty string if no name has been
		/// specified in the constructor.

	static ActiveThreadPool& defaultPool();
		/// Returns a reference to the default
		/// thread pool.

private:
	ActiveThreadPool(const ActiveThreadPool& pool);
	ActiveThreadPool& operator = (const ActiveThreadPool& pool);

private:
	std::unique_ptr<ActiveThreadPoolPrivate> _impl;
};

} // namespace Poco


#endif // Foundation_ActiveThreadPool_INCLUDED