现在的位置: 首页 > 综合 > 正文

linux多线程之线程资源的释放

2018年03月21日 ⁄ 综合 ⁄ 共 1308字 ⁄ 字号 评论关闭

     一般来说,对一段运行代码进行加锁然后解锁,如下所示:

  pthread_mutex_lock(&mutex);

  //运行代码;

  pthread_mutex_unlock(&mutex);

  如果在运行代码这块发生错误,有异常,导致这个线程异常退出,那么怎么办,pthread_unlock没有得到调用,那么这个锁资源没有解锁。可以用下面的方法修改。

  pthread_cleanup_push(pthread_mutex_unlock, (void *) &mutex);

  pthread_mutex_lock(&mutex);

  /* do some work */

  pthread_mutex_unlock(&mutex);

  pthread_cleanup_pop(0);

  这样假如运行代码发生错误时没有调用到解锁,pthread_cleanup_up会自动来调用,参数为0表示不执行push进来的函数。

  但是如果是异常错误的话,这个参数并不影响异常终止时清理函数的执行。

  必须要注意的是,如果线程处于PTHREAD_CANCEL_ASYNCHRONOUS状态,上述代码段就有可能出错,因为CANCEL事件有可能在pthread_cleanup_push()和pthread_mutex_lock()之间发生,或者在pthread_mutex_unlock()和pthread_cleanup_pop()之间发生,从而导致清理函数unlock一个并没有加锁的mutex变量,造成错误。因此,在使用清理函数的时候,都应该暂时设置成PTHREAD_CANCEL_DEFERRED模式。为此,POSIX的Linux实现中还提供了一对不保证可移植的pthread_cleanup_push_defer_np()/pthread_cleanup_pop_defer_np()扩展函数,功能与以下代码段相当:

  { int oldtype;

  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);

  pthread_cleanup_push(routine, arg);

  ...

  pthread_cleanup_pop(execute);

  pthread_setcanceltype(oldtype, NULL);

  }

  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  设置退出类型pthread_setcanceltype

  #include <pthread.h>

  int pthread_setcanceltype(int type, int *oldtype);

  返回值:函数成功返回0。任何其他返回值都表示错误。

  将线程退出类型设置为延迟类型或异步类型。参数type的取值为PTHREAD_CANCEL_DEFERRED或PTHREAD_CANCEL_ASYNCHRONOUS。

  当一个线程被创建后,缺省值是延迟类型。在异步方式下,线程可以在执行的任何时候被退出。

抱歉!评论已关闭.