- Derive a subclass from the class
POCO::Runnable. Implement the inheritedrun()method. - Create objects from the subclass you have derived. Each object is correspondent to a runnable task.
- Create a ThreadPool, and start the runnable tasks using the method
start(Runnable & target)of ThreadPool. - You can use the method
joinAll()of ThreadPool to wait for all threads in the pool to complete.
Here is a simple example.
- #include <iostream>
- #include "Poco/Runnable.h"
- #include "Poco/ThreadPool.h"
- class Task : public Poco::Runnable{
- private:
- int _id;
- public:
- Task (int number) : _id(number) {}
- void run(){
- for (int i= 0; i < 100; i++){
- std::cout << i << ") Task " << _id << " is working" << std::endl;
- }
- }
- };
- int main(){
- Task task1(1);
- Task task2(2);
- Poco::ThreadPool pool;
- pool.start(task1);
- pool.start(task2);
- pool.joinAll();
- return 0;
- }
By default, the maximum thread pool size is 16. You can set this parameter according to your needs using an appropriate ThreadPool constructor call.
After the creation of a ThreadPool, you can change the maximum capacity using the method
void addCapacity(int n);n may be a positive or a negative number.
Default Arguments
One of ThreadPool's constructors is declared as follows:ThreadPool(int minCapacity = 2, int maxCapacity = 16, int idleTime = 60, int stackSize = 0);C++ allows the programmer to specify default values for arguments in the function's declaration. If arguments are missing in the invocation of the function, the default values are used. For example, invoking
POCO::ThreadPool pool(2,10) sets the values of minCapacity to 2, maxCapacity to 10, and takes the default values for idleTime and stackSize, that are 60 and 0.
Basic synchronization
Basic synchronization is performed using the lock object POCO::RWLock . If several threads share a resource, they should also share a lock. When a thread needs to access the shared resources, it should acquire the lock first by executing the method writeLock() of POCO::RWLock . When it finishes with the resource, it releases the lock by executing the method unlock() . Only one thread is allowed to acquire the lock in any point in time. Other threads trying to acquire the lock when it is already acquired by some thread, will wait untill it is released.
The next example corrects the synchronization problem from the previous example. (The shared resource is the output stream.)
- #include <iostream>
- #include "Poco/Runnable.h"
- #include "Poco/ThreadPool.h"
- #include "Poco/RWLock.h"
- class Task : public Poco::Runnable{
- private:
- int _id;
- Poco::RWLock * _lock;
- public:
- Task (int id, Poco::RWLock * lock) : _id(id), _lock(lock) {}
- void run(){
- for (int i= 0; i < 100; i++){
- _lock->writeLock(); //acquire lock
- std::cout << i << ") Task " << _id << " is working" << std::endl;
- _lock->unlock(); //release lock
- }
- }
- };
- int main(){
- Poco::RWLock lock;
- Task task1(1, &lock);
- Task task2(2, &lock);
- Poco::ThreadPool pool;
- pool.start(task1);
- pool.start(task2);
- pool.joinAll();
- return 0;
- }
References
A Guided Tour of the POCO C++ LibrariesPOCO Threading documentation