Boost logo

Boost Users :

From: bill_kempf (williamkempf_at_[hidden])
Date: 2002-03-20 17:11:47


--- In Boost-Users_at_y..., "gunnarnl" <gunnar_at_m...> wrote:
> After overcoming my initial problems with compiling I have another
> question. I like very much the thread_group concept. My idea is to
> create different groups running different type of tasks. E.g. one
> group deals with user request tasks and another deal with responses
> from a mainframe etc. In addition, I think it would be good to be
> able to maximize the number of threads running concurrently in a
> single group. In this way one could tune the processes regarding to
> the number of CPUs and so that not one single group gets more CPU
> usage than others etc. I guess I'm not the first one to think about
> these things. Does anyone have any ideas about these type of task
> groups?

It sounds to me like what you want isn't a thread_group, but a
thread_pool. All the thread_group is is a convenient way to manage
the lifetime of multiple threads. A thread_pool, on the other hand,
not only manages the lifetime but also the operation of the threads.
With a thread_pool you simply add jobs and the thread_pool dispatches
the job to a running thread if it's available, possibly spawns a new
thread if there isn't an available thread, or adds the job to a queue
to be processed by the next available thread. Elaborate thread pools
allow you to control things like the minimum and maximum number of
threads that will be spawned to handle the jobs and how long a thread
should be allowed to sit idle waiting for another job before it self
terminates.

There are plans to add a thread_pool to Boost.Threads. In fact, a
prototype is nearly ready to be uploaded to the main Boost list for
comments (it's actually being worked on by someone else, but I've had
some input on it).

> Although it doesn't nearly contain what I want to do, I got the
> following test work nicely.
>
>
> #include <boost/thread/thread.hpp>
> #include <boost/thread/xtime.hpp>
> #include <iostream>
> #include <windows.h>
>
> template<class T>
> class thread_task
> {
> public:
> thread_task(T* obj) : m_obj(obj)
> {
> assert(m_obj != 0);
> }
> virtual ~thread_task() { }
> void operator() ()
> {
> m_obj->setup();
> m_obj->run();
> m_obj->cleanup();
>
> delete m_obj;
> }
> private:
> T* m_obj;
> };
>
> class my_task
> {
> public:
> my_task() {}
> void setup()
> {
> std::cout << "setup " << GetCurrentThreadId() <<
> std::endl;
> }
> void run()
> {
> std::cout << "Start sleeping in thread " <<
> GetCurrentThreadId() << std::endl;
> boost::xtime xt;
> boost::xtime_get(&xt, boost::TIME_UTC);
> xt.sec += 1;
>
> boost::thread::sleep(xt);
> std::cout << "Woke up " << GetCurrentThreadId() <<
> std::endl;
>
> }
> void cleanup()
> {
> std::cout << "cleanup " << GetCurrentThreadId() <<
> std::endl;
> }
> };
>
> int main(int argc, char* argv[])
> {
>
> boost::thread_group threads;
> int i;
> for (i = 0; i < 10; ++i)
> {
> my_task* aTask = new my_task();
> thread_task<my_task> new_task(aTask);
> threads.create_thread(new_task);
> }
> threads.join_all();
>
> return 0;
> }

This looks more like the classic example of the usage of
thread_group, leaving me to wonder precisely what it is you want to
accomplish. This code doesn't illustrate capping the number
of "tasks", for instance. It's also a bit more complex then what I
would have done, though it does work.

> By the way, is there an operation in boost::thread to get the
current
> thread id?

No, because not all platforms have any concept of thread id. I may
add something along these lines soon, though it won't be precisely
what you expect, I think. The ID won't be an integer, though it will
be comparable.

Bill Kempf


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net