我正在使用boost
库通过boost::thread
类在c ++中创建线程。
我有以下课程,其中将void routine (int)
定义为boost::thread
例程:
A
类:
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; }
};
和main
函数:
int main()
{
A a;
myThread = new boost::thread(&A::routine, a, 6);
myThread->join();
return 0;
}
在main
中,我创建一个A
的对象,并且构造函数被调用一次,但是出了点问题。在这种情况下,destructor
函数也应该仅被调用一次,但是它被调用了几次,并且得到以下结果:
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
有什么建议吗?