Here is my test program using boost::thread. I would expect this program to crash inside FunctionA because m_ClassA is no longer available.
Can anybody help me to understand why it does not crash? thank you.
[root@localhost]# uname -a
Linux localhost.localdomain 2.6.9-22.EL #1 Mon Sep 19 18:20:28 EDT 2005 i686 i686 i386 GNU/Linux
[root@localhost]# g++ --version
g++ (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ -I/opt/third_party_libraries/boost_1_33_1 -L/opt/third_party_libraries/boost_1_33_1/stage/lib -lboost_thread-gcc-mt main.cpp -o test
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
void SleepTime( int a )
{
boost::xtime xt;
boost::xtime_get( &xt, boost::TIME_UTC );
xt.sec += a;
boost::thread::sleep( xt );
}
class ClassA
{
public:
ClassA( int member ) : m_member( member )
{
std::cout << "ClassA constructor: " << m_member << std::endl;
}
virtual ~ClassA( )
{
std::cout << "ClassA destructor: " << m_member << std::endl;
}
void SetMember( int a )
{
m_member = a;
}
int GetMember() const
{
return m_member;
}
private:
int m_member;
};
class TestClass
{
public:
TestClass( )
: m_ClassA( 0 )
{
std::cout << "TestClass constructor: " << m_ClassA.GetMember() << std::endl;
}
virtual ~TestClass( )
{
std::cout << "TestClass destructor: " << m_ClassA.GetMember() << std::endl;
}
void RunThreadFunction()
{
std::cout << "RunThreadFunction start" << std::endl;
boost::thread thr( boost::bind( &TestClass::ThreadFunction, this ) );
std::cout << "RunThreadFunction end" << std::endl;
}
void ThreadFunction( )
{
std::cout << "ThreadFunction start" << std::endl;
m_ClassA.SetMember( 5 );
SleepTime( 5 );
FunctionA( );
return;
}
void FunctionA( )
{
std::cout << "Inside FunctionA: " << m_ClassA.GetMember() << std::endl;
}
private:
ClassA m_ClassA;
};
int main()
{
{
TestClass a;
a.RunThreadFunction();
}
std::cout << "main wait" << std::endl;
SleepTime( 10 );
return 0;
}