#include
int pthread_mutex_lock ( pthread_mutext_t *mutex );
int pthread_mutext_trylock ( phtread_mutext_t *mutex );
int pthread_mutext_unlock ( pthread_mutext_t *mutex );
如果线程试图对同一个互斥量加锁两次,那么它自身就会陷入死锁状态。
3.读写锁
读写锁非常适合对数据结构读的次数远大于写的次数的情况。
#include
int pthread_rwlock_init ( pthread_rwlock_t *restrict rwlock , const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy ( pthread_rwlock_t *restrict rwlock );
int pthread_rwlock_rdlock( pthread_rwlock_t *rwlock );
int pthread_rwlock_tryrdlock( pthread_rwlock_t *rwlock );
int pthread_rwlock_wrlock( pthread_rwlock_t *rwlock );
int pthread_rwlock_trywrlock( pthread_rwlock_t *rwlock );
int pthread_rwlock_unlock( pthread_rwlock_t *rwlock );