473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

boost::thread and class d'tor

Why this strange output?
Why so many d'tor calls?

The code:
----------------------------------------------------------
#include <cstdio>
#include <boost/thread/thread.hpp>

class A
{
boost::thread *thread;
public:
A() : thread(0) {
printf("ctor\n");
}
~A() {
printf("dtor enter\n");
join();
//delete thread;
printf("dtor leave\n");
}

void createThread() {
printf("create enter\n");
thread = new boost::thread(*this);
printf("create leave\n");
}

void operator()() {
printf("start thread: %d\n", thread);
delete thread; thread = 0;
printf("leave thread\n");
}

void join() { if (thread) thread->join(); }
};

int main() {
A a;
a.createThread();
a.join();
return 0;
}
----------------------------------------------------------

The output:
----------------------------------------------------------
ctor
create enter
dtor enter
dtor leave
dtor enter
dtor leave
dtor enter
dtor leave
dtor enter
dtor leave
dtor enter
dtor leave
dtor enter
dtor leave
dtor enter
dtor leave
start thread: 0
dtor enter
dtor leave
leave thread
dtor enter
dtor leave
create leave
dtor enter
dtor leave
dtor enter
dtor leave
----------------------------------------------------------

Totally mess!
Jan 7 '08 #1
2 2316
On 2008-01-07 08:21:16 -0500, Chameleon <ch******@hotmail.comsaid:
Why this strange output?
Why so many d'tor calls?
The immediate answer is that class A's copy constructor isn't
instrumented, so the output doesn't show copy construction of objects
that later get destroyed.

The more useful answer is, you should probably ask the Boost folks. It
certainly looks like there are more objects being created than are
necessary.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 7 '08 #2
On Jan 7, 2:41 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-01-07 08:21:16 -0500, Chameleon <cham_...@hotmail.comsaid:
Why this strange output?
Why so many d'tor calls?
The immediate answer is that class A's copy constructor isn't
instrumented, so the output doesn't show copy construction of objects
that later get destroyed.
The more useful answer is, you should probably ask the Boost
folks. It certainly looks like there are more objects being
created than are necessary.
Possibly, but if his compiler doesn't implement RVO or NRVO,
there will be quite a few.

More important, perhaps, is the fact that all of these instances
point to the same instance of boost::thread. And that setting
it to null after the delete in the operator()() isn't going to
null the pointer to in the instance in main. If the thread
actually starts immediately, and operator()() gets executed
before the code gets to the a.join() in main, he's going to call
join() on a deleted object (undefined behavior). And if the
thread with main executes first, then in the operator()(), he's
going to destruct a boost::thread on which another thread is
waiting for a join---I'm not sure what that's supposed to do
either.

So just instrumenting the copy constructor is not enough. He's
going to have to think about joinability, and either use a local
boost::thread object in the createThread function (detached
thread) or use a boost::shared_ptr rather than a raw pointer to
the boost::thread in his object (and of course, in neither case
delete the boost::thread manually himself).

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dingounan | last post by:
how to compile boost.thread,and set it? I have trouble. platform: windows XP compiler: Borland C++ Compiler 5.5.1 commandline tools thanks.
1
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am using a boolean variable to indicate when the...
4
by: Lighter | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; using namespace boost; void hello() { cout << "Hello world, I'm a thread!" << endl; }
5
by: linyanhung | last post by:
I used a boost multi thread in VS 2005 on a Duo Core PC, and made a two thread process. The code is something like this: #include <boost/thread/thread.hpp> void fun1() { //do something
3
by: Gary Wessle | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; class waiter { public: waiter(); void waiting(); void preform();
4
by: Gary Wessle | last post by:
Hi given the Boost thread example here http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/thread.cpp the code below attempts to run the example thread while...
2
by: Hans Mull | last post by:
Hi! I'm experimenting with boost::thread. I have a GUI application with a slot function: void someFunction(){...} void Frame::OnOkButtonClick(...) { switch(someInteger) { case 0: break
3
by: Lars Uffmann | last post by:
I have this wxWidgets OnButtonClick event handler, that apparently holds a lock on all widgets in my form, but this event handler is supposed to end a thread in the background - while that thread...
1
by: Boogie | last post by:
Hi, A specific problem arises when I try to use boost::thread 1.35.0 with Borland Turbo 2006. Code below compiles but when run it throws (in commented line - thread creation): "assertion...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.