473,320 Members | 1,900 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,320 software developers and data experts.

Doubt about boost shared_ptr

Hi all,
I have a query regarding the exchanging of a
boost::shared_ptr beween different threads. In my program i've two
threads both of which having their own internal queues for storing the
shared_ptr. one thread is meant to pass the shared_ptr to another after
its processing.

So when a message arrives i convert it into a class of
my own called 'Msg' and i put the 'Msg' object pointer into a
shared_ptr and put into the other thread's message queue. in that
second thread after starting the processing suddenly the program stops
responding. when i examined the log files and tried tracing, i noticed
that the 'shared_ptr' is getting destroyed just after it has been
released by the first thread after putting it into the second thread's
message queue. I am passing the shared_ptr as reference.

anybody knows what cud be the reason? or is it
that shared_ptr is not meant to work that way?

Sreehari

Dec 13 '06 #1
7 2936

myfavd...@gmail.com wrote:
I am passing the shared_ptr as reference.

anybody knows what cud be the reason? or is it
that shared_ptr is not meant to work that way?
I'm no expert on threads or boost::sherd_ptr but I would guess you
should pas it by value, not by reference. That should bump the use
count when its copied and keep it alive.

It would proably be best to try the boost users glist though.

regards
Andy Little

Dec 13 '06 #2
my*******@gmail.com wrote:
Hi all,
I have a query regarding the exchanging of a
boost::shared_ptr beween different threads. In my program i've two
threads both of which having their own internal queues for storing the
shared_ptr. one thread is meant to pass the shared_ptr to another after
its processing.

So when a message arrives i convert it into a class of
my own called 'Msg' and i put the 'Msg' object pointer into a
shared_ptr and put into the other thread's message queue. in that
second thread after starting the processing suddenly the program stops
responding. when i examined the log files and tried tracing, i noticed
that the 'shared_ptr' is getting destroyed just after it has been
released by the first thread after putting it into the second thread's
message queue. I am passing the shared_ptr as reference.

anybody knows what cud be the reason? or is it
that shared_ptr is not meant to work that way?
You're doing something wrong but it's not clear from your description what.
One thing is share_ptr needs locking or some other form of syncrhonization
to be shared between threads. The other is it's not clear how it being
put onto the second thread's queue. Are you putting a copy of the
shared_ptr onto the queue or a reference to the shared ptr onto the queue?
Sharing raw references to shared_ptr is as problematic as sharing raw
references to any object.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Dec 13 '06 #3
my*******@gmail.com wrote:
Hi all,
I have a query regarding the exchanging of a
boost::shared_ptr beween different threads. In my program i've two
threads both of which having their own internal queues for storing the
shared_ptr. one thread is meant to pass the shared_ptr to another after
its processing.

So when a message arrives i convert it into a class of
my own called 'Msg' and i put the 'Msg' object pointer into a
shared_ptr and put into the other thread's message queue. in that
second thread after starting the processing suddenly the program stops
responding. when i examined the log files and tried tracing, i noticed
that the 'shared_ptr' is getting destroyed just after it has been
released by the first thread after putting it into the second thread's
message queue. I am passing the shared_ptr as reference.

anybody knows what cud be the reason? or is it
that shared_ptr is not meant to work that way?
I think Kwikius nailed it. In general, shared_ptr objects should be
passed by value, so they can properly manage the controlled resource. If
your queue is holding a reference, that doesn't count: when the
shared_ptr object that it refers to goes out of scope the reference has
nothing to refer to.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 13 '06 #4
Thanks everyone. I was passing the shared_ptr using reference at first.
Then i changed to pass by value. And now it seems to be working fine.
Can somebody give me how the destructor is
actually gets called? i mean, exactly when and by which thread calls it
in case of a multi-threaded program?

Regards,
Sreehari

Pete Becker wrote:
my*******@gmail.com wrote:
Hi all,
I have a query regarding the exchanging of a
boost::shared_ptr beween different threads. In my program i've two
threads both of which having their own internal queues for storing the
shared_ptr. one thread is meant to pass the shared_ptr to another after
its processing.

So when a message arrives i convert it into a class of
my own called 'Msg' and i put the 'Msg' object pointer into a
shared_ptr and put into the other thread's message queue. in that
second thread after starting the processing suddenly the program stops
responding. when i examined the log files and tried tracing, i noticed
that the 'shared_ptr' is getting destroyed just after it has been
released by the first thread after putting it into the second thread's
message queue. I am passing the shared_ptr as reference.

anybody knows what cud be the reason? or is it
that shared_ptr is not meant to work that way?

I think Kwikius nailed it. In general, shared_ptr objects should be
passed by value, so they can properly manage the controlled resource. If
your queue is holding a reference, that doesn't count: when the
shared_ptr object that it refers to goes out of scope the reference has
nothing to refer to.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 14 '06 #5
my*******@gmail.com wrote:
Thanks everyone. I was passing the shared_ptr using reference at first.
Then i changed to pass by value. And now it seems to be working fine.
Can somebody give me how the destructor is
actually gets called? i mean, exactly when and by which thread calls it
in case of a multi-threaded program?
When the last shared_ptr object that manages a particular resource is
destroyed, the resource also gets destroyed.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 14 '06 #6
my*******@gmail.com wrote:
Thanks everyone. I was passing the shared_ptr using reference at first.
Then i changed to pass by value. And now it seems to be working fine.
Can somebody give me how the destructor is
actually gets called? i mean, exactly when and by which thread calls it
in case of a multi-threaded program?
When the share_ptr drops its reference by being assigned another value,
and it sees the previous object's reference count drop to zero. So any place
you assign another value to the shared_ptr could potentially call the
dtor and you need to ensure that the dtor is safe to invoke in those
places.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Dec 14 '06 #7
Here is a good alternative to Boost's non-atomic shared pointer:

http://appcore.home.comcast.net/vzoom/refcount/

?

Currently, Boost doesn't provide support for atomic reference counting;
shared_ptr<Tfalls under 'basic' thread-safety. I propose a reference
counting algorithm that falls under 'strong' thread-safety. Here is a
experimental prototype I created:

http://appcore.home.comcast.net/vzoom/refcount/
A SPARC 32-64 version is underway. Here is some more information on my
algorithm:

http://groups.google.com/group/comp....8717d3bcdedfe9
(initial idea; pseudo-code)

http://groups.google.com/group/comp....21a151d3916592
(mostly lock-free...)

http://groups.google.com/group/comp....22ef08ae26e2f3
(async-signal-safe aspects of my algorithm)

http://groups.google.com/group/comp....7b1867c19e6288
(async-signal...)

http://groups.google.com/group/comp....e468f341a33ee2
(adding more async-signal-safety characteristics'...)

http://groups.google.com/group/comp....a46f3ef24b786a
http://groups.google.com/group/comp....63f874241bcaf4
(possible improvements...)
Does anybody think that Boost could possible benefit from this level of
thread-safety? Any thoughts?


Thank you all for you time!

Here are some more links:

http://search.gmane.org/?query=&auth...+thomasson---A
--
Chris Thomasson

http://appcore.home.comcast.net/
(portable lock-free data-structures)
Dec 15 '06 #8

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

Similar topics

6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
0
by: eric | last post by:
Can I declare a pure virtual member function which accepts as input a boost shared pointer to an object of a base class, such that - concrete implementations of the function can redirect the...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
5
by: number774 | last post by:
I've used Boost for this example; in fact we have our own pointer class, for historic reasons. #include "boost\shared_ptr.hpp" // A heirarchy of classes class G1 {}; class G2: public G1 {};...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
by: varnie | last post by:
hell-o !~ i faced weird problem during usage boost::pool_allocator. after i've changed: typedef boost::shared_ptr< param > ParamPtr; typedef std::vector< ParamPtr > ParamPtrVector; with:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.