C++マルチスレッドサブクラス「部分解析」の問題
C++マルチスレッドサブクラス"部分解析"問題
reference
解析関数がマルチスレッドであるC++のスレッドで安全なオブジェクトコールバックに遭遇した場合
Race condition in rtc::Thread::Clear()
Lifetime
Object Lifetime Manager A Complementary Pattern for Controlling Object Creation and Destruction
#include
#include
using namespace std;
/*
* This is a simple example used to reveal memory issue when we destruct a object of child class.
*1.Set two break points as shown below.
*2.Watch the memory of "ch".
*3.Start the exmple.
* Observe the memory content of "ch", we can get the following conclusions:
* When we break at ~child(), the memory of "ch" is still valid.
* The moment we break at ~Base(), the "child" memory part of "ch" becomes invalid but still the "base" memory
* part is valid.
* Knowing this feature is especially important when we program under muti-thread.
*/
class Base
{
public:
Base()
{
name = "base object";
cout << "Base()" << name << endl;
}
~Base()
{
// set a break point here.
cout << "~Base() start " << name << endl;
}
string name;
};
class child :public Base
{
public:
child()
{
cName = "child object";
cout << "child()" << cName <// set a break point here.
cout << "~child() start " << cName << endl;
}
string cName;
};
child *ch = new child();
int main()
{
delete ch;
return 0;
}
reference
解析関数がマルチスレッドであるC++のスレッドで安全なオブジェクトコールバックに遭遇した場合
Race condition in rtc::Thread::Clear()
Lifetime
Object Lifetime Manager A Complementary Pattern for Controlling Object Creation and Destruction