|
Boost Users : |
Subject: [Boost-users] question about boost::thread
From: PingShan Li (lipingshan72_at_[hidden])
Date: 2008-09-12 18:06:39
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_at_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_at_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;
}
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