views:

150

answers:

5

There is a long-time request and is called from the "main" (UI) thread. It is planned to move it's call into a separate thread. The problem is that some objects are created in this thread on the heap (main thread will have to work with these pointers).

Questions:

  1. Is it allowed to delete 'another-thread' objects in the main thread?
  2. Is it a good idea to delete object in "another" thread.

Thanks

+3  A: 
  1. Yes.

  2. Depending on situation, this is not bad and not good, just do what you need according to your algorithm.

Deleting objects created in another thread may be dangerous only if object destructor works with a thread local storage. This must be mentioned in the class documentation.

Alex Farber
AFAIK, there's nothing in the C++ standard that forbids `new` and `delete` using a per-thread heap (and in fact there are some allocators that work this way), so I think there is no single word answer to (1).
Ben Voigt
@Ben: There is nothing in the standard about threads at all. The current standard pretents that there are no threads.
sbi
+1  A: 

There is nothing to prevent you from doing that, although I wouldn't advise for it. You'd better use a shared_ptr or similar object IMHO.

Hosam Aly
+1  A: 

It is safe, but don't forget about race conditions. Delete it like this:

//someObj

   if (someObj != null)
   {
      lock();
         if (someObj != null)
         {
            delete someObj;
            someObj = NULL;
         }
      unlock();
   }
Andrey
Who is 'lock', and what is he doing with with 'someObj'?
Christopher
@Christopher: he meant "create mutex, grab it before nuking the object, then release after deleting the object". Or critical section instead of mutex.
SigTerm
@Christopher by lock i meant "use some sync mechanism", SigTerm explained it right.
Andrey
That code is still dangerous. Double checked locking is a bad idea. Just lock and delete if not null. Think of the situation where thread 1 deletes the object and set it to null (assume you have made the variable volatile and the compiler understands what this means). Npw before thread 1 releases the lock(), thread 2 actually checks the correctly cached value and rushes passed the if (because it is now null) and executes the following code which then thread 1 executes immediately afterwords. Is your code still going to work? Depends what it is.
Martin York
@Martin York you are wrong. this code is as safe as it could be. first, if you use `volatile` it prevents reading from cache. second is that `lock()` usually (depends on implementation) forces fence instruction which again refresh the cache. read more here: http://en.wikipedia.org/wiki/Double-checked_locking
Andrey
@andrey: I understand how the locking should work, but read the description above in more detail as thread 2 never hits a lock and that is what makes it dangerous for any subsequent code. But I am making the assumption that the following code would be dangerous for thread 1 if already executed by thread 2 (which in my experience it usually is)
Martin York
A: 

It is a allowed to delete in another thread.
However: it is the programmer's responsability to make sure that the owner is always known. Also, in multithreaded environment, you have to make sure that there are no racing conditions, where another thread still tries to access the object. However, this is also true when the creating thread deletes the object. A good way to solve this is with shared/weak pointers; the boost shared_ptr is thread-safe

stefaanv
A: 

You may want to take a look at Boost's shared_ptr. It will handle freeing objects for you, no matter in which thread they were created, and it also saves you the trouble of keeping track of which threads hold a pointer to which objects. It is also totally thread safe (you'll still have to protect the inner workings of your own object, but the rest it will take care for you).

Gianni