美文网首页C++
对于多线程的锁的异常处理

对于多线程的锁的异常处理

作者: 凹大猫的鱼 | 来源:发表于2019-04-11 16:59 被阅读8次

对于多线程的锁来说,如果某个线程获取了这个锁,但该线程挂掉了,然后锁就不会释放了。对于这种问题,只需要在锁初始化的时候设置锁的属性。

pthread_mutexattr_t mutexattr;
pthread_mutexattr_t mutexattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_INHERIT);
pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
pthread_mutexattr_setrobust_np(&mutexattr,PTHREAD_MUTEX_STALLED_NP);
pthread_mutex_init(&(g_halog_shm_ptr->ha_log_shm_mutex), &mutexattr);

pthread_mutexattr_setrobust_np

PTHREAD_MUTEX_ROBUST_NP

When the owner of the mutex dies, all subsequent calls to pthread_mutex_lock() are blocked from progress in an unspecified manner.

PTHREAD_MUTEX_STALLED_NP

When the owner of the mutex dies, the mutex is unlocked. The next owner of this mutex acquires it with an error return of EOWNWERDEAD.

pthread_mutexattr_setrobust_np

PTHREAD_PRIO_NONE

When a thread owns a mutex with the PTHREAD_PRIO_NONE protocol attribute, its priority and scheduling shall not be affected by its mutex ownership.
线程的优先级不会因为锁的拥有而改变。

PTHREAD_PRIO_INHERIT

使用PTHREAD_PRIO_INHERIT,持有锁的线程会继承当前争用锁的最高线程优先级,如果没有其它线程争用,那优先级不会变化。

PTHREAD_PRIO_PROTECT

这个和PTHREAD_PRIO_INHERIT 的区别在于不会因为其它的线程而变化,而是根据锁的属性的优先级来确定的,可以通过pthread_mutexattr_setprioceiling 来设置锁的优先级。

void ha_log_lock_shm_mutex(void)
{
  int lock_result = pthread_mutex_lock(&g_halog_shm_ptr->ha_log_shm_mutex);
        /*如果返回EOWNERDEAD则调用pthread_mutex_consistent_np统一锁的状态。然后unlock再lock。如果返回ENOTRECOVERABLE,则pthread_mutex_destroy销毁,然后再重新init锁。*/
    if(lock_result == EOWNERDEAD){
        if(pthread_mutex_consistent_np(&g_halog_shm_ptr->ha_log_shm_mutex) == 0){
            pthread_mutex_unlock(&g_halog_shm_ptr->ha_log_shm_mutex);
            pthread_mutex_lock(&g_halog_shm_ptr->ha_log_shm_mutex);
        }else{
            pthread_mutex_destroy(&g_halog_shm_ptr->ha_log_shm_mutex);
            ha_log_init_shm_mutex();
            pthread_mutex_lock(&g_halog_shm_ptr->ha_log_shm_mutex);
        }
      }else{
            if(lock_result == ENOTRECOVERABLE){
                pthread_mutex_destroy(&g_halog_shm_ptr->ha_log_shm_mutex);
                ha_log_init_shm_mutex();
                pthread_mutex_lock(&g_halog_shm_ptr->ha_log_shm_mutex);
            }
      }
}

相关文章

  • 对于多线程的锁的异常处理

    对于多线程的锁来说,如果某个线程获取了这个锁,但该线程挂掉了,然后锁就不会释放了。对于这种问题,只需要在锁初始化的...

  • iOS线程同步

    线程同步 提到多线程大家肯定会提到锁,其实真正应该说的是多线程同步,锁只是多线程同步的一部分。 多线程对于数据处理...

  • 浅析乐观锁、悲观锁与CAS

    乐观锁与悲观锁 处理多线程并发访问最常用的就是加锁,锁又分成乐观锁和悲观锁。 悲观锁 在多线程访问共享资源时,同时...

  • Java多线程: 如何捕获多线程中的异常

    你处理过多线程中异常吗?如何捕获多线程中发生的异常?捕获子线程的异常与捕获当前线程的异常一样简单吗? 除了try ...

  • 2021-03-01 深度剖析并发锁之AQS设计思想及原理

    深度剖析并发锁之AQS设计思想及原理 了解锁的场景和基本原理 多线程并发处理,没有同步导致结果异常本质上是由于++...

  • iOS底层探索之多线程(十四)—关于@synchronized锁

    对于多线程你了解多少?对于锁你又了解多少?锁的原理你又知道吗? iOS底层探索之多线程(一)—进程和线程[http...

  • iOS底层探索之多线程(十五)—@synchronized源码分

    对于多线程你了解多少?对于锁你又了解多少?锁的原理你又知道吗? iOS底层探索之多线程(一)—进程和线程[http...

  • 如何减少上下文切换

    无锁并发编程:多线程竞争锁时,会引起上下文切换,所以多线程处理数据时,可以用一些办法来避免使用锁,如将数据的 ID...

  • 2019-01-20

    异常处理,自定义注册异常,多线程基础知识整理 1.异常 1.1异常的概念 异常:在程序编译完成和执行程序过程中,出...

  • Java多线程异常处理

    线程异常处理 Java中每个线程的异常处理是相互独立的,一个线程产生的异常不会影响其他线程的正常运行。因此,也不能...

网友评论

    本文标题:对于多线程的锁的异常处理

    本文链接:https://www.haomeiwen.com/subject/mfdoiqtx.html