How to correctly destroy pthread mutex

后端 未结 4 711
天命终不由人
天命终不由人 2021-02-09 05:26

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

相关标签:
4条回答
  • 2021-02-09 05:53

    I couldn't agree more with caf on this. We have done something similar in certain implementation (e.g. refer ifData_createReference & ifData_removeReference routines in ifMIB.c). The basic idea is keeping a global lock to guard the entire object list and an object level lock for guarding individual entry in the list.

    When we have to create a new entry in the list, take WRITE lock on the list and add a new entry, so that entry is added consistently to all the users of the list. And release the list lock.

    When we have to look-up/access an entry from the list, take a READ lock on the list and search for the entry. Once we find the entry, take object lock in READ mode for read-only operations / take object lock in WRITE mode for modifying the object entry. Now, release the list lock. Now once we are done with processing of the object entry release the object lock as well.

    When the object entry has to be removed from the list, take a WRITE lock on the list. Search and find the object entry in the list. Take a WRITE lock on the object entry, this will ensure that you are the ONLY current user for the object. Now remove the entry from the listing, as no one can search it any more in the list. And release the object lock immediately. Then, release the list lock. Now destroy the object and release the object resources.

    0 讨论(0)
  • 2021-02-09 05:54

    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).

    0 讨论(0)
  • 2021-02-09 05:56

    I want to free the object from the cache. Now how to destroy/free mutex correctly ? pthread_mutex_destroy document says we should not use the pthread_mutex_destroy while the mutex is locked. Lets say a thread decides to destroy the object it needs to destroy the lock so it releases the lock and does a pthread_mutex_destroy. What happens to the other threads waiting for the objects lock ?

    Well I hope I get your intention right, I had the exact same problem. Anyway I realized, later that I was stupid: Complaining about undefined behaviour of pthread_mutex_* functions after pthread_mutex_destroy() is like complaining about SEGFAULTS when accessing a pointer after free().

    Most C programs are modelled around the paradigm that every program must make sure that memory is not accessed after some sort of destruction. Good C programs will have a design that prevents pointers from being spread everywhere, so that destruction happens only at well defined places, when no other variable contains a pointer anymore. This is not at all a concern in garbage collected languages.

    Solution 1: Use refcounting like it is done for memory allocation. The refcounter is accessed via atomic functions. (Use the glib, it contains great, portable stuff)

    Solution 1b: Use refcounting like it is done for memory allocation, sperate the kinds of workers that are important from those that aren't and use weak references in the later so that they do not prevent object destruction.

    Solution 2: Do not destroy the mutex. Why bother with saving RAM? Just make a global static array of like 128k objects. Add a struct member wich indicates the state of the object. Instead of destruction just atomic compare and set the state variable, and print an error in the threads that access an object in "DISABLED" state.

    Solution 3 - The hard way: Don't do shared memory concurrency. Combine a thread pool which matches the number of CPUs on the system, use non-blocking IO, message objects and state-machine design. Make message queues for each task, and let tasks communicate only by messages enqueued in the queue of the other. Put the queue in the same 'select' or 'pollfd' set that contains the sockets/filedescriptors. To shuffle big data (3d game) between state machines, use a struct with an atomic refcounter and copy on write semantics. This will in most cases be the most performant, stable and maintainable solution.

    If what you do has anything to do with performance, think twice about using the atomic operations. They can be more expensive than mutexes.

    0 讨论(0)
  • 2021-02-09 05:57

    It's undefined behavior to (a) attempt to destroy a locked mutex, or (b) reference a destroyed mutex other than to call pthread_mutex_init to recreate it (See documentation). That means that the thread that destroys your shared mutex is going to race with the others locking it, and either (1) destroy happens first, other threads invoke undefined behavior trying to lock because of (b) or (2) lock in another thread happens first and destroying thread invokes undefined behavior because of (a).

    You need to change your design so that a mutex under active contention is never destroyed. For your example, you could destroy the shared mutex in main after all threads are joined. For the program you describe, you probably need to insert a reference count in the objects.

    0 讨论(0)
提交回复
热议问题