1.锁
pthread_mutex_t 互斥锁
pthread_rwlock_t 读写锁
优先级
可以添加读写优先级
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_setkind_np (&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);//写优先.引[1]
(void)pthread_rwlock_init(&rwlock, &attr);
android中使用bionic/libc/include/pthread.h, 和linux比也是阉割的,只有下面两个
enum {
PTHREAD_RWLOCK_PREFER_READER_NP = 0, //读优先,默认值
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 1, //写优先
};
写优先
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
Setting the lock kind to this avoids writer starvation as long as any read locking is not done in a recursive fashion. 设置之后写等待的时间 不会比读等待的时间长,但是递归中无法生效。
第一句话这样理解:
如果读等待是提前申请的,那么依然比写等待有更高的优先级“
2.多线程设计注意要点
1.重点内容.static成员变量 要加全局的的锁保护
static成员变量在不同的对象里相当于是同一个变量
2.同一线程两次访问 同一共享变量,全程要加一个锁
中间阶段可能被其他线程修改
3.线程创建
线程函数是static的函数,故而个如果定义在类成员函数中,则该线程函数不能访问this指针。需要在create thread的时候通过thread参数传入类的指针。
pthread_create(fun,0,this);
1.http://www.man7.org/linux/man-pages/man3/pthread_rwlockattr_setkind_np.3.html