If i have a class that is supposedly threadsafe, then how do i ensure that
the destructor isnt called concurrently with any other member function of
that class - or is this ensured by C++ that this wont happen (tho' i dont
see how).
Critical sections and the like just won't cut it.
TIA,
-fj
No, C++ doesn't care about threads. You will have to make sure that
before an object is destroyed, no other threads will access that thread.
You will have to resolve this issue at a higher level.
Though mutexes can be used to prevent concurrent access to an object, it
doesn't solve the destructor problem. Let's say thread A is busy
destructing the object, and thread B calls a member function on the same
object. Mutexes can block thread B until the destructor running in
thread A has finished, but by the time thread B is unblocked the object
is already destroyed
Further questions regarding multithreaded programming are best asked in
comp.programming.threads.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Thread safe? Just like with exception safety, there's multiple "levels"
of thread safety. I call it "unsafe", "basic", and "strong".
http://groups.google.com/groups?selm=3ECD2B7D.BBC526FC%40web.de
(Subject: Re: shared_ptr/weak_ptr and thread-safety)
> then how do i ensure that
> the destructor isnt called concurrently with any other member function of
> that class - or is this ensured by C++ that this wont happen (tho' i dont
> see how).
It isn't ensured by C++. You shall simply synchronize it.
regards,
alexander.
--
Posted via http://dbforums.com
Well, the real issue is just as you put it. I have a class factory handing
out pointers to objects across multiple threads, and the class-factory
itself is responsible for deleting the objects. Now i really dont mind
having invalid references out there, because the way the semantics work, the
threads should query the class factory *every time* before using the object
<i know this is really dumb...but reality sucks!>
No, the issue is that if i'm inside an object method, while the factory
decides to shut down, then i'm screwed <and it seems to be happening quite
often in a test case - so i'm quite majorly screwed already>. I just can't
figure how to work this out.
-fj
Synchronize the shutdown. It's always a good idea. Well, as for the
factory and deletion, the following might provide you with some ideas:
http://groups.google.com/groups?selm=3D021EA4.E2217C09%40web.de
(Subject: Re: Objects in container: creation and deletion details)
regards,
alexander.