Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
39a3e6c
make Poco::ActiveThreadPool easy to use (#4544)
siren186 Aug 2, 2024
acdb9ba
code format
siren186 Aug 6, 2024
b407b83
Fix ThreadSanitizer thread leak error
siren186 Aug 6, 2024
6d70ff8
enh(ActivePooledThread): Change pointers to references
siren186 Aug 7, 2024
d2744fa
enh(ActivePooledThread): remove unused method
siren186 Aug 7, 2024
df5c140
enh(Poco::ActiveThreadPool): Use std::unique_ptr instead of raw pointer
siren186 Aug 21, 2024
fde68aa
enh(Poco::ActiveThreadPool): Use C++ static_cast instead of C casting
siren186 Aug 21, 2024
754e628
enh(Poco::ActiveThreadPool): Use standard containers instead of imple…
siren186 Aug 21, 2024
9717b53
enh(Poco::ActiveThreadPool): Change pointer to reference
siren186 Aug 21, 2024
4a3340b
enh(Poco::ActiveThreadPool): Use smart pointers instead of bare pointers
siren186 Aug 21, 2024
e215cb7
enh(Poco::ActiveThreadPool): Fix codeql warning: A stack address whic…
siren186 Aug 21, 2024
86d9a13
enh(Poco::ActiveThreadPool): More test case
siren186 Aug 21, 2024
cba4673
enh(Poco::ActiveThreadPool): std::optional::value unavailable on earl…
siren186 Aug 22, 2024
6bed6cb
enh(Poco::ActiveThreadPool): Fix compare function for make heap
siren186 Aug 22, 2024
7b26ab8
enh(Poco::ActiveThreadPool): Add more test case
siren186 Aug 22, 2024
81823a4
enh(Poco::ActiveThreadPool): Add more test case
siren186 Aug 22, 2024
36e1ce5
enh(Poco::ActiveThreadPool): Code style
siren186 Aug 22, 2024
3637022
enh(Poco::ActiveThreadPool): Test case
siren186 Aug 22, 2024
a37bf5a
enh(Poco::ActiveThreadPool): Test case
siren186 Aug 22, 2024
09db4e7
enh(Poco::ActiveThreadPool): Fix test case error
siren186 Aug 23, 2024
3a76e28
Revert "enh(Poco::ActiveThreadPool): std::optional::value unavailable…
matejk Aug 24, 2024
7491582
enh(macOS): require min deployment macOS version 10.15 which has full…
matejk Aug 24, 2024
8c88376
Merge branch 'pocoproject:main' into main
siren186 Aug 26, 2024
94c4301
Merge remote-tracking branch 'remotes/upstream/4624-make-it-easy-to-u…
siren186 Aug 26, 2024
32e1d33
enh(Poco::ActiveThreadPool): Remove useless "{}"
siren186 Aug 26, 2024
40bedc9
enh(Poco::ActiveThreadPool): Rename member variable m_impl to _impl
siren186 Aug 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 21 additions & 68 deletions Foundation/include/Poco/ActiveThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,35 @@

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


namespace Poco {


class Runnable;
class ActiveThread;
class ActiveThreadPoolPrivate;


class Foundation_API ActiveThreadPool
/// A thread pool always keeps a number of threads running, ready
/// to accept work.
/// Threads in an active thread pool are re-used
/// Every thread in the pool has own notification-queue with Runnable
/// Every Runnable executes on next thread (round-robin model)
/// The thread pool always keeps fixed number of threads running.
/// 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
/// Round-robin model allow efficiently utilize cpu cores
{
public:
ActiveThreadPool(int capacity = static_cast<int>(Environment::processorCount()) + 1,
int stackSize = POCO_THREAD_STACK_SIZE);
/// Creates a thread pool with fixed capacity threads.
/// Creates a thread pool with a maximum thread count of capacity.
/// Threads are created with given stack size.

ActiveThreadPool(std::string name,
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 fixed capacity threads.
/// Creates a thread pool with the given name and a maximum thread count of capacity.
/// Threads are created with given stack size.

~ActiveThreadPool();
Expand All @@ -64,39 +61,19 @@ class Foundation_API ActiveThreadPool
int getStackSize() const;
/// Returns the stack size used to create new threads.

void start(Runnable& target);
/// Obtains a thread and starts the target.
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, const std::string& name);
void start(Runnable& target, int priority = 0);
/// Obtains a thread and starts the target.
/// Assigns the given name to the thread.

void startWithPriority(Thread::Priority priority, Runnable& target);
/// Obtains a thread, adjusts the thread's priority, and starts the target.

void startWithPriority(Thread::Priority priority, Runnable& target, const std::string& name);
/// Obtains a thread, adjusts the thread's priority, and starts the target.
/// Assigns the given name to the thread.

void stopAll();
/// Stops all running threads and waits for their completion.
///
/// Will also delete all thread objects.
/// If used, this method should be the last action before
/// the thread pool is deleted.
///
/// Note: If a thread fails to stop within 10 seconds
/// (due to a programming error, for example), the
/// underlying thread object will not be deleted and
/// this method will return anyway. This allows for a
/// more or less graceful shutdown in case of a misbehaving
/// thread.

void joinAll();
/// Waits for all threads to complete.
///
/// Note that this will join() underlying
/// threads and restart them for next tasks.
/// 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,
Expand All @@ -107,38 +84,14 @@ class Foundation_API ActiveThreadPool
/// Returns a reference to the default
/// thread pool.

protected:
ActiveThread* getThread();
ActiveThread* createThread();

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

typedef std::vector<ActiveThread*> ThreadVec;

std::string _name;
int _capacity;
int _serial;
int _stackSize;
ThreadVec _threads;
mutable FastMutex _mutex;
std::atomic<size_t> _lastThreadIndex{0};
private:
std::unique_ptr<ActiveThreadPoolPrivate> m_impl;
};


inline int ActiveThreadPool::getStackSize() const
{
return _stackSize;
}


inline const std::string& ActiveThreadPool::name() const
{
return _name;
}


} // namespace Poco


Expand Down
Loading