View Full Version : Threading Question

05-20-2007, 07:37 PM
I have a certain data type which I'm working on making thread-safe.

Now, accesses are controlled through the use of a CL_Mutex. Every member function which accesses the data is structured like this:

type class::function(blah) {

As I understand it, this should make all data accesses work correctly.

Now, for the destructor, I want to make sure that no threads are accessing the data while it gets destroyed. I'm currently using this method:

class:~class() {

Note that I never leave the mutex. This should mean that no other thread ever gets to access this data after it's destroyed. Is that correct?

05-31-2007, 10:22 PM
No, since if another class had locked the mutex, the lock would break when the mutex is destroyed. After that they most likely will access a destroyed object.

06-02-2007, 06:31 PM
Right, I forgot to post a follow-up to this when I figured it out.

Anyway, for the particular object I'm thinking of, I can wait to make sure that all other threads have terminated, and *then* destroy this object.

However, for objects that need to be destroyed while other threads are still running, I'm thinking of using a combination of reference counters and status flags.