How exactly i can destroy a pthread mutex variable ?
Here is what i want to do. I want to have objects (structure variables) cached , which are looked up by key. I want
The basic problem you have is that removing an object from the cache is something that requires synchronisation at the cache level, not the object level.
One way to implement this is by having a global lock for the entire cache that is only held during lookups, and is dropped once the object lock has been acquired. This lock can be a reader-writer lock, held for writing only if a thread is going to remove the object. So a thread that wishes to use a cache object would do:
pthread_rwlock_rdlock(&cache_lock);
exampleObj * obj = Lookup(key);
pthread_mutex_lock(&obj->mutex);
pthread_rwlock_unlock(&cache_lock);
/* Do some work on obj */
pthread_mutex_unlock(&obj->mutex);
and a thread that wishes to destroy a cache object would do:
pthread_rwlock_wrlock(&cache_lock);
exampleObj * obj = Lookup(key);
pthread_mutex_lock(&obj->mutex);
Remove(key);
pthread_rwlock_unlock(&cache_lock);
/* Do some cleanup work on obj */
pthread_mutex_unlock(&obj->mutex);
pthread_mutex_destroy(&obj->mutex);
(where the Remove()
function removes the function from the cache so that subsequent Lookup()
functions cannot return it).