0

I'm using of boost library to create a thread in c++ by boost::thread class.
I have the following class in which defined a void routine (int) as a boost::thread routine :

A class :

class A
{
public:
    // Constructor
    A() { cout << "this is constructor" << endl; }

    void routine(int num)
    {
        for(int i = 0; i < num; i++)
        {
            // do stuffs
            cout << num + i << endl;
            Sleep(1000);
        }
    }

    // Destructor
    ~A() { cout << "this is destructor" << endl; }
};

And main function :

int main()
{
    A a;
    myThread = new boost::thread(&A::routine, a, 6);

    myThread->join();

    return 0;
}

In the main I create an object of A and constructor function is call once, but something is wrong. in this situation the destructor function should be call just once too, but it called several times and the following result is obtained :

this is constructor
this is destructor
this is destructor
this is destructor
this is destructor
this is destructor
this is destructor
this is destructor
6
7
8
9
10
11
this is destructor
this is destructor

Any suggestion?

0 Answers0