Boost logo

Boost Users :

From: Duane Murphy (duanemurphy_at_[hidden])
Date: 2002-12-12 15:05:49


The application I am working on uses a number of threads to do work. In
some cases pointers from one thread (usually the "main" thread) are
passed to a worker thread. The worker thread will use this pointer to
handle processing of the results (in various ways).

The problem that I am concerned about is lifetime control. Objects in the
main thread could be deleted before the worker thread has a chance to use
them. This of course would be a "bad thing."

I am considering investigating the use of weak_ptr as a potential
solution to this problem. The basic idea would be to pass weak_ptr's to
the worker thread. The worker thread can then check the validity of the
weak_ptr before accessing it.

Can weak_ptr be used without using shared_ptr? It seems like they are
closely tied. I was hoping to use weak_ptr without forcing the use of
shared_ptr.

Are there other solutions to this problem, besides the obvious "Dont let
it happen." :-)

[ Note that we are not yet using boost::threads so this is a more general
question about managing inter-thread object lifetimes. ]

Thanks for your tips and suggestions,

 ...Duane


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net