经典指数          
原因
1759
浏览数
0
收藏数
 

请用普通的互斥锁编程实现一个读写锁

     举报   纠错  
 
切换
1 个答案

下面是可参考的伪代码:

count_mutex = mutex_init();

write_mutex = mutex_init();

read_count = 0;

void read_lock {

lock(count_mutex);

read_count++;

if (read_count == 1) {

lock(write_mutex);

}

unlock(count_mutex);

}

void read_unlock {

lock(count_mutex);

read_count--;

if (read_count == 0) {

unlock(write_mutex);

}

unlock(count_mutex);

}

void write_lock {

lock(write_mutex);

}

void write_unlock {

unlock(write_mutex);

}

 
切换
撰写答案