473,624 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doubt about boost shared_ptr

Hi all,
I have a query regarding the exchanging of a
boost::shared_p tr 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 2965

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_pt r 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_p tr 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_p tr 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_p tr 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<Tfal ls 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
9037
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 type:
0
1391
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 pointer to a new object - clients of the function can pass in as input a boost shared pointer to an object of a derived class ? Consider the following classes in the problem domain:
2
4824
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 "B.h"
2
2129
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 namespace System; #pragma unmanaged
6
4008
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
1
5556
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
1540
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 {}; // A method which takes a shared pointer to the base class
4
3945
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
1832
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
8242
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.