Poco

class ActiveThreadPool

Library: Foundation
Package: Threading
Header: Poco/ActiveThreadPool.h

Description

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. 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

Member Summary

Member Functions: capacity, createThread, defaultPool, getStackSize, getThread, joinAll, name, start, startWithPriority, stopAll

Constructors

ActiveThreadPool

ActiveThreadPool(
    int capacity = static_cast < int > (Environment::processorCount ())+ 1,
    int stackSize = 0
);

Creates a thread pool with fixed capacity threads. Threads are created with given stack size.

ActiveThreadPool

ActiveThreadPool(
    std::string name,
    int capacity = static_cast < int > (Environment::processorCount ())+ 1,
    int stackSize = 0
);

Creates a thread pool with the given name and fixed capacity threads. Threads are created with given stack size.

Destructor

~ActiveThreadPool

~ActiveThreadPool();

Currently running threads will remain active until they complete.

Member Functions

capacity

int capacity() const;

Returns the capacity of threads.

defaultPool static

static ActiveThreadPool & defaultPool();

Returns a reference to the default thread pool.

getStackSize inline

int getStackSize() const;

Returns the stack size used to create new threads.

joinAll

void joinAll();

Waits for all threads to complete.

Note that this will join() underlying threads and restart them for next tasks.

name inline

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.

start

void start(
    Runnable & target
);

Obtains a thread and starts the target.

start

void start(
    Runnable & target,
    const std::string & name
);

Obtains a thread and starts the target. Assigns the given name to the thread.

startWithPriority

void startWithPriority(
    Thread::Priority priority,
    Runnable & target
);

Obtains a thread, adjusts the thread's priority, and starts the target.

startWithPriority

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.

stopAll

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.

createThread protected

ActiveThread * createThread();

getThread protected

ActiveThread * getThread();