http://sourceware.org/ml/glibc-bugs/2008-11/msg00073.html
http://www.nabble.com/-Bug-nptl-7057--New:-pthread-rwlock-does-not-implement-'writer-preferred'-option-td20703744.html
pthread rwlock does not implement 'writer preferred' option
linux 读写锁
定义(全局变量):
pthread_rwlock_t rw_lock;
初始化:
{
pthread_rwlockattr_t attr;
int ret = 0;
ret = pthread_rwlockattr_init( &attr );
if( ret < 0 )
{
...;
return;
}
#ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP); // 写优先
#endif
ret = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); // 进程内
if( ret < 0 )
{
...;
return;
}
ret = pthread_rwlock_init( &rw_lock, &attr );
if( ret < 0 )
{
...;
return;
}
ret = pthread_rwlockattr_destroy( &attr );
if( ret < 0 )
{
...;
return;
}
return;
}
使用:
// 写锁
pthread_rwlock_wrlock(&rw_lock);
......
pthread_rwlock_unlock(&rw_lock);
// 读锁
pthread_rwlock_rdlock(& w_lock);
......
pthread_rwlock_unlock(&rw_lock);
另外一个:
#define _XOPEN