• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Delete an object in concurrent situation

 
David Freeman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks
I just stuck at a very simple problem in concurrent application in cpp.
so the problem is, in a parallel application, I want to delete an object at a time, but I don't know when should I delete it, because this is possible just after deleting the object the blocked thread wants to use it. i other word I want to know how many threads are holding the reference at a same time, and the hard thing is deleting the object is done by a thread-safe function.

for example



A friend told me to use std::shared_ptr, but if I be honest, I really cannot figure out how would I use it in this situation.
thanks in advanced.
 
Luke Leber
Ranch Hand
Posts: 69
2
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure sharing access to the object is the best course of action?

Personally, I would use thread-local storage and forget about the problem entirely if the cost of making a few copies isn't that outrageous.

en.cppreference.com/w/cpp/language/storage_duration

The C++ standard guarantees that a shared_ptr implementation is thread-safe, however makes absolutely no guarantees about the thread-safety of objects accessed through them.



Remember, C++ is not C
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic