Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

互斥量(锁)

  • 引用

    #include <pthread.h>
    
    
  • 全局声明

    pthread_mutex_t mutex;
    
  • 初始化锁

    int pthread_mutex_init(pthread_mutex_t* mutex, const phread_mutexattr_t* attr);
    
  • 请求加锁

    int pthread_mutex_lock(pthread_mutex_t* mutex);
    
    • 进入临界区前调用
  • 释放锁

    int pthread_mutex_unlock(pthread_mutex_t* mutex);
    
    • 离开临界区后调用
    • 一定注意区别仅在于unlock,不能写成lock再次请求加锁
  • 销毁锁

    int pthread_mutex_destroy(pthread_mutex_t* mutex);