Pthreads lock recovery

I am working on a multi-threaded network server application. At the moment, I have problems restoring the lock. If a thread unexpectedly freezes when it holds a lock, say, a mutex, rwlock, spinlock, etc., is it possible to restore the lock from another thread without having to enter the lock structure and manually disconnect the owner from the lock. I would not want to go to this extreme to clear it, as this will make the code not portable. I tried to get the owner of the castle owner to do pthread_kill in the violating thread and see the return code. But even using the mutex type attribute PTHREAD_MUTEX_ERRORCHECK, I still cannot get control of the mutex from another thread if the terminating thread is completed. This can be a problem if any internal table is updated when the thread is freed.since ultimately this will cause the entire server application to stop.

I used Google extensively and I get conflicting information, even here. Any suggestions or ideas I can research?

This is on FreeBSD 9.3 using the clang-llvm compiler.

0
source share
1 answer

For mutexes shared between processes ( PTHREAD_PROCESS_SHARED), you can set them PTHREAD_MUTEX_ROBUST... but you are stuck in the problem that the state protected by the mutex might be invalid - depending on the application.

For mutexes that are not shared between processes, there is no standard concept of "reliability", since a thread cannot spontaneously die on its own - a thread will work until it is canceled, it will not exit, or the process will not end or die.

:

  void pthread_cleanup_push(void (*routine)(void*), void *arg);
  void pthread_cleanup_pop(int execute);

, , - - :

  pthread_mutex_lock(&foo) ;      // as now
  pthread_cleanup_push(pthread_mutex_unlock, &foo) ;  // extra step

  ....

  pthread_cleanup_pop(true) ;     // replacing the pthread_mutex_unlock()

: , , , !!

, , , , /, / ( ).

+1

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


All Articles