Threads and virtual functions

Hello, here is a simplified code I wrote: class A{ public: A(); ~A(); void Run(); void ThreadA(); virtual void HandleSomething(); }; void A::HandleSomething() { } void A::ThreadA() { if(condition) { HandleSomething(); } } void A::Run() { boost::thread thread(&A::ThreadA,this); } This is my base class. As you can see the thread calls the virtual function when the condition becomes true. I use the virtual function in my derived class so everytime the condition becomes true the function HandleSomething() should do something. So far, so good but when I try to change some variables in the derived class the program crashes without any warning or error message. I use the latest Ubuntu version with GCC 4.5.3 and boost 1.46.1.

make sure that you join on the thread or else the thread object might be destructed before the thread having finished. Date: Thu, 7 Jul 2011 00:19:14 +0200 From: geoffrey@kroetentunnel.dyndns.org To: boost-users@lists.boost.org Subject: [Boost-users] Threads and virtual functions Hello, here is a simplified code I wrote: class A{ public: A(); ~A(); void Run(); void ThreadA(); virtual void HandleSomething(); }; void A::HandleSomething() { } void A::ThreadA() { if(condition) { HandleSomething(); } } void A::Run() { boost::thread thread(&A::ThreadA,this); } This is my base class. As you can see the thread calls the virtual function when the condition becomes true. I use the virtual function in my derived class so everytime the condition becomes true the function HandleSomething() should do something. So far, so good but when I try to change some variables in the derived class the program crashes without any warning or error message. I use the latest Ubuntu version with GCC 4.5.3 and boost 1.46.1. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

class A{ public: A(); ~A(); void Run(); void ThreadA(); virtual void HandleSomething(); };
void A::HandleSomething() {
}
void A::ThreadA() { if(condition) { HandleSomething(); } }
void A::Run() { boost::thread thread(&A::ThreadA,this); }
This is my base class. As you can see the thread calls the virtual function when the condition becomes true. I use the virtual function in my derived class so everytime the condition becomes true the function HandleSomething() should do something. So far, so good but when I try to change some variables in the derived class the program crashes without any warning or error message. I use the latest Ubuntu version with GCC 4.5.3 and boost 1.46.1.
Do you call the virtual function in a constructor?

Am 07.07.2011 00:19, schrieb Geoffrey:
Hello, here is a simplified code I wrote:
class A{ public: A(); ~A(); void Run(); void ThreadA(); virtual void HandleSomething(); };
void A::HandleSomething() {
}
void A::ThreadA() { if(condition) { HandleSomething(); } }
void A::Run() { boost::thread thread(&A::ThreadA,this); }
This is my base class. As you can see the thread calls the virtual function when the condition becomes true. I use the virtual function in my derived class so everytime the condition becomes true the function HandleSomething() should do something. So far, so good but when I try to change some variables in the derived class the program crashes without any warning or error message. I use the latest Ubuntu version with GCC 4.5.3 and boost 1.46.1.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I do not call the virtual function in a constructor. I forgot to mention that ThreadA never stops because it runs in a while loop.
participants (3)
-
bit bit
-
Geoffrey
-
Igor R