s The Nana Programmer's Guide
Nana C++ Library  

An open-source C++ framework project
The Programmer's Guide 
nana::threads::pool

Description

A thread pool manages a group threads for a large number of tasks processing.

Model of

None

Public base classes

None

Members

pool() The default constructor. It creates a group of threads.
pool(unsigned threadsize) Creates a number of threads specifed by threadsize.
~pool() The destructor. It waits for the all running tasks till they are finished and skips all the queued tasks.
template<typename Function>
void push(const Function& fn) volatile
Adds a task to the task queue.
void signal() volatile Sends a signal.
void wait_for_signal() volatile Waits for a signal until the signal processed.

File

nana/threads/pool.hpp

Notes

1, The signal()/wait_for_signal() provide a method to wait for tasks to finish.

    void foo()
    {}

    void foo2()
    {
        nana::system::sleep(1000);
        std::cout<<"This is foo2"<<std::endl;
    }

    int main()
    {
        nana::threads::pool pool;
        for(int i = 0; i < 10; ++i)
            pool.push(foo);
        pool.signal();          //Make a signal and the signal will be triggered when the
                                //tasks which are pushed before it are finished.
        pool.push(foo2);
        pool.wait_for_signal(); //Wait until the signal is triggered. The wait might be finished
                                //before finish of foo2, because the signal is made before pushing foo2.
    }



See also

pool_pusher, A Method to prevent UI from blocking while busy.


Move to The Nana Programmer's Guide Main Page