Use lock algorithm for shared memory

I want to use the lock-free algorithm for shared memory to exclude a mutex. I have some processes in which shared data uses shared memory. If a process blocks a mutex and crashes, all other processes also fail.

I read several articles that implement a blocking algorithm with a linked list. But in my shared memory, I cannot determine the data structure to use in this memory block. I have a pointer to this block.

Therefore, I have no idea how to apply the blocking algorithm in my situation. I need help from you. Thank you and sorry if my English is very bad.

+4
source share
1 answer

If a process blocks a mutex and crashes, all other processes also fail.

In particular, there are reliable mutexes for this use case:

PTHREAD_MUTEX_ROBUST

If a process containing an owned thread of a trusted mutex terminates while holding a mutex lock, the next thread that receives the mutex should be notified of completion with the return value [EOWNERDEAD] from the lock function. If the native thread of the trusted mutex terminates while holding the lock of the mutex, the next thread that receives the mutex can be notified of the end using the return value [EOWNERDEAD]. Then the notified thread may try to mark the state protected by the mutex as sequential again by calling pthread_mutex_consistent (). After the subsequent successful call to pthread_mutex_unlock (), the mutex lock should be released and can usually be used by other threads. If the mutex is unlocked without calling pthread_mutex_consistent (), it must be in an unusable state, and all attempts to lock the mutex must fail [ENOTRECOVERABLE]. The only valid operation on such a mutex is pthread_mutex_destroy ().

+1
source

Source: https://habr.com/ru/post/1432658/


All Articles