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
From: "Duane Murphy"
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.
No, weak_ptr cannot be used without shared_ptr. Why do you need to avoid shared_ptr?
participants (2)
-
Duane Murphy
-
Peter Dimov