互斥鎖(mutex)
通過鎖機制實現線程間的同步
1、初始化互斥鎖;
int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutex attr_t *mutexattr); //mutex:鎖容器; mutexattr:鎖的屬性NULL;
2、申請鎖,如果鎖不可?,阻塞等待
int pthread_mutex_lock(pthread_mutex_t *mutex); //mutex:你要申請的鎖 返回值:0成功-1失敗
3、測試鎖 如果鎖不可?,立即返回
int pthread_mutex_trylock(pthread_mutex_t *mutex);
5、銷毀鎖 int pthread_mutex_destroy(pthread_mutex_t *mutex);
互斥鎖參考代碼