boost::thread() calls destructor!!
Hello, I tried to modify the sample code from Bill Kempf for boost.thread, listing 4 that I got at the following link http://www.cuj.com/documents/s=8470/cuj0205kempf/list4.htm to embed the write function in a writeClass function-object. That is a test because in a program I need to pass a function object to one thread, and not a global function. I simply wrote this code: class writerClass { public: ~writerClass() { // for Debug std::cout << "writerClass dtor" << std::endl; } void operator() () { for (int n = 0; n < ITERS; ++n) { { boost::mutex::scoped_lock lock(io_mutex); std::cout << "sending: " << n << std::endl; } buf.put(n); } } }; and changed in the main() --> boost::thread thrd2(&writer); with --> boost::thread thrd2((writerClass())); Everything works the same, but I have the destructor of wirterClass called 4 times, when constructing thrd2!!! That's weird to me, but it's annoying because in my other program, the dtor destroys some member data, and the program crashes because of it. Why is the ~writerClass() called? is there maybe some exception thrown?? how can I solve the problem? thank you ricky P.S. if it can be useful, during the compilation I get a warning (MSVC 7.1), but I read that it can be safely ignored: warning C4275: non dll interface class "boost::noncopyable_::noncopyable" used as a base for dll interface class "boost::mutex" [...]\boost\noncopyable.hpp(22): see declaration of "boost::noncopyable_::noncopyable" [...]\boost\thread\mutex.hpp(33): see declaration of "boost::mutex"
Hi, I don't pretend to know how the boost thread functions work, but I've used them and haven't had any problems. Are you sure it's not just that the object is being copied 4 times and what you're seeing are the destructors for the copies? In your test code, you wouldn't be able to tell. According to the docs, the thread constructor makes at least one copy of its argument. Does your class have a working copy constructor? cheers, /Patrik Ricky Corsi wrote:
Hello,
I tried to modify the sample code from Bill Kempf for boost.thread, listing 4 that I got at the following link http://www.cuj.com/documents/s=8470/cuj0205kempf/list4.htm
to embed the write function in a writeClass function-object. That is a test because in a program I need to pass a function object to one thread, and not a global function.
I simply wrote this code:
class writerClass { public: ~writerClass() { // for Debug std::cout << "writerClass dtor" << std::endl; }
void operator() () { for (int n = 0; n < ITERS; ++n) { { boost::mutex::scoped_lock lock(io_mutex); std::cout << "sending: " << n << std::endl; } buf.put(n); } } };
and changed in the main() --> boost::thread thrd2(&writer); with --> boost::thread thrd2((writerClass()));
Everything works the same, but I have the destructor of wirterClass called 4 times, when constructing thrd2!!!
That's weird to me, but it's annoying because in my other program, the dtor destroys some member data, and the program crashes because of it. Why is the ~writerClass() called? is there maybe some exception thrown?? how can I solve the problem?
thank you ricky
P.S. if it can be useful, during the compilation I get a warning (MSVC 7.1), but I read that it can be safely ignored:
warning C4275: non dll interface class "boost::noncopyable_::noncopyable" used as a base for dll interface class "boost::mutex"
[...]\boost\noncopyable.hpp(22): see declaration of "boost::noncopyable_::noncopyable" [...]\boost\thread\mutex.hpp(33): see declaration of "boost::mutex"
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
!DSPAM:42983bbe6827497323079!
Patrik Jonsson wrote:
Hi,
I don't pretend to know how the boost thread functions work, but I've used them and haven't had any problems. Are you sure it's not just that the object is being copied 4 times and what you're seeing are the destructors for the copies? In your test code, you wouldn't be able to tell. According to the docs, the thread constructor makes at least one copy of its argument. Does your class have a working copy constructor?
Since boost::thread takes a boost::function, which copies it's functor when it is copied to preven dangling references - so in the second case writerClass is probably indeed being copied. 4 times does seem slightly odd I'll admit... but I suppose the boost::function is simply getting passed around a bit by value internally. If you create a copy constructor, it should show writerClass getting copied a few times. If you didn't want this behavior you'd use boost::ref IIRC: writerClass writer; boost::thread thrd2( boost::ref( writer ) );
and changed in the main() --> boost::thread thrd2(&writer); with --> boost::thread thrd2((writerClass()));
Everything works the same, but I have the destructor of wirterClass called 4 times, when constructing thrd2!!!
That's weird to me, but it's annoying because in my other program, the dtor destroys some member data, and the program crashes because of it. Why is the ~writerClass() called? is there maybe some exception thrown?? how can I solve the problem?
Again, probably due to the fact that writerClass is getting copied a few times. Adding an appropriate copy constructor should do the trick (e.g. deep copy pointed to data or use a boost::shared_ptr so that all the classes share a single version of the data, but don't delete it until the last instance dies) -Mike
participants (3)
-
Michael B. Edwin Rickert
-
Patrik Jonsson
-
Ricky Corsi