Boost logo

Boost Users :

From: Paul J R (me_at_[hidden])
Date: 2008-02-10 09:55:32


Hi all,

I had a question about boost threads as im using them currently in a
project. I've written a very simple implementation of what im trying to do
and the result I get, but im having trouble understanding why:

mytest.cpp:

class A {
    public:
        A() {}
        ~A() {}
        virtual void worker() {
                std::cout << "I am A worker" << std::endl;
        }
        virtual A *factory() {
                std::cout << "Factory for A class called" << std::endl;
                return new A;
        }
        virtual void operator()() {
                std::cout << "Calling A operator()" << std::endl;
                worker();
        }
        void threadcontroller() {
                boost::thread_group thre;
                for(;;) {
                        std::cout << "I am in the A.threadcontroller()
method" << std::endl;
                        A *mea = factory();
                        mea->worker();
                        thre.create_thread(*mea);
                        exit(0);
                }
        }
};

class B: public A {
    public:
        B() {}
        ~B() {}
        virtual void worker() {
                std::cout << "I am B worker" << std::endl;
        }
        virtual void operator()() {
                std::cout << "Calling B operator()" << std::endl;
                worker();
        }
        B *factory() {
                std::cout << "Factory for B class called" << std::endl;
                return new B;
        }
};

int main()
{
        B meb;

        meb.worker();
        meb.threadcontroller();
}

Output:
$ ./mytest
I am B worker
I am in the A.threadcontroller() method
Factory for B class called
I am B worker
Calling A operator()
I am A worker

The first output is from the meb.worker() call in main(), the second is
from meb.threadcontroller. the thrid is the factory call from within
threadcontroller, the forth is mea.worker() in thread controller, then the
next 2 are from after the thread_group.create_thread() call. Once its in
the thread it seems to revert to class A's methods.

Now, i know if i overload the threadcontroller class in B it'd solve all
my problems, but the real threadcontroller method is alot more complex and
class A has about 6 sub classes so replicating the threadcontroller in
each of the classes would be a little tedious.

Have i stumbled across a bug, or is that how its supposed to work?

Thanks in advance


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