深圳百度推广竞价托管,德州鲁企动力网站优化中心,ck整合插件wordpress,哪个网站做音基的题不花钱介绍
shared_mutex即读写锁#xff0c;不同与我们常用的独占式锁mutex#xff0c;shared_mutex是共享与独占共存的锁#xff0c;实现了读写锁的机制#xff0c;即多个读线程一个写线程#xff0c;通常用于对于一个共享区域的读操作比较频繁#xff0c;而写操作比较少的情…介绍
shared_mutex即读写锁不同与我们常用的独占式锁mutexshared_mutex是共享与独占共存的锁实现了读写锁的机制即多个读线程一个写线程通常用于对于一个共享区域的读操作比较频繁而写操作比较少的情况。读写锁比起mutex具有更高的适用性具有更高的并行性可以有多个线程同时占用读模式的读写锁但是只能有一个线程占用写模式的读写锁读写锁的基本规则可以总结为“写优先读共享交叉互斥“具体表现为读写锁的三种状态1当读写锁是写加锁状态时在这个锁被解锁之前所有试图对这个锁加锁的线程都会被阻塞。交叉互斥 2当读写锁在读加锁状态时所有试图以读模式对它进行加锁的线程都可以得到访问权但是以写模式对它进行加锁的线程将会被阻塞。读共享交叉互斥 3当读写锁在读模式的锁状态时如果有另外的线程试图以写模式加锁读写锁通常会阻塞随后的读模式锁的请求这样可以避免读模式锁长期占用而等待的写模式锁请求则长期阻塞。写优先
注其实在读者-写者问题中有读者优先和写者优先两种模式只是在boost中的shared_mutex默认的实现是写者优先这其实也是有道理的因为在我们总是希望读到的数据是最新的这就得保证写者优先。下面通过一个 boost::shared_mutex的应用实例来反应其锁机制该例子中我们建立多个读者线程两个写者线程程序的实际运行结果很直接的反应了shared_mutex应用情况。
代码
#include boost/thread/thread.hpp
#include boost/ref.hpp
#include stringboost::shared_mutex global_mutex;
int global_num 10;//全局变量写者改变全局变量读者读全局变量//读线程
void read_thread(std::string name){boost::shared_lockboost::shared_mutex lock(global_mutex);//读锁定 shared_lockprintf(线程%s抢占了资源global_num %d\n,name.c_str(),global_num);boost::this_thread::sleep(boost::posix_time::seconds(1));printf(线程%s释放了资源...\n,name.c_str());
}//写线程
void write_thread(std::string name){boost::unique_lockboost::shared_mutex lock(global_mutex);//写锁定 unique_lockglobal_num;//写线程改变数据的数值printf(线程%s抢占了资源global_num %d\n,name.c_str(),global_num);boost::this_thread::sleep(boost::posix_time::seconds(1));printf(线程%s释放了资源...\n,name.c_str());}
int main(){std::string read_thread_r1 read_thread_r1;std::string read_thread_r2 read_thread_r2;std::string read_thread_r3 read_thread_r3;std::string read_thread_r4 read_thread_r4;std::string read_thread_r5 read_thread_r5;std::string write_thread_w1 write_thread_w1;std::string write_thread_w2 write_thread_w2;boost::thread_group tg;tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r1)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r2)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r3)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r4)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r5)));tg.create_thread(boost::bind(write_thread,boost::ref(write_thread_w1)));tg.create_thread(boost::bind(write_thread,boost::ref(write_thread_w2)));tg.join_all();return 0;
}
程序运行结果 结论
从运行结果中可以看出如果是读者获得了shared_mutex则其他读者可以同时抢占资源但如果是写者获得了shared_mutex则其他的写者或读者都不能进入临界区即同时只有一个写者能进入临界区。