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 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 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. 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)
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)
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) 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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:
|
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"
|
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
|
by: Toby Bradshaw |
last post by:
Hi,
Consider the following:
class A
{
public:
virtual bool foo() = 0;
};
| |
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;
|
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
|
by: EnsGabe |
last post by:
Suppose you have a class heirarchy as such:
class Base{
....
};
class Mid1 : public Base{
....
|
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:
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |