• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            歲月流轉(zhuǎn),往昔空明

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              118 Posts :: 3 Stories :: 413 Comments :: 0 Trackbacks
            沒有什么實際意義(boost.threads已經(jīng)加入了讀寫鎖好像),只是說明一下condition的用法。
            鎖由Mutex和Lock構(gòu)成。
            Mutex是由condition實作而成的,Lock和ScopedLock區(qū)別不是很大,分讀寫鎖而已。

            以下是Mutex的代碼
            class?read_write_mutex
            {
            ????mutex?mtx_;
            ????
            int?read_cnt_;
            ????condition?cond_;

            public:
            ????read_write_mutex():?read_cnt_(
            0){}
            ????
            ~read_write_mutex(){}

            ????
            void?lock_read(){
            ????????mutex::scoped_lock?
            lock(mtx_);
            ????????
            while(read_cnt_?<?0){
            ????????????cond_.wait(
            lock);
            ????????}

            ????????
            ++read_cnt_;
            ????}

            ????
            ????
            void?unlock_read(){
            ????????mutex::scoped_lock?
            lock(mtx_);
            ????????
            --read_cnt_;
            ????????cond_.notify_all();
            ????}


            ????
            void?lock_write(){
            ????????mutex::scoped_lock?
            lock(mtx_);
            ????????
            while(read_cnt_?!=?0){
            ????????????cond_.wait(
            lock);
            ????????}

            ????????read_cnt_?
            =?-1;
            ????}


            ????
            void?unlock_write(){
            ????????mutex::scoped_lock?
            lock(mtx_);
            ????????read_cnt_?
            =?0;
            ????????cond_.notify_all();
            ????}

            }
            ;

            以下是鎖的代碼
            class?scoped_rlock
            {
            ????read_write_mutex
            &?cond_;
            public:
            ????scoped_rlock(read_write_mutex
            &?c):cond_(c){cond_.lock_read();}
            ????
            ~scoped_rlock(){cond_.unlock_read();}
            }
            ;

            class?scoped_wlock
            {
            ????read_write_mutex
            &?cond_;
            public:
            ????scoped_wlock(read_write_mutex
            &?c):cond_(c){cond_.lock_write();}
            ????
            ~scoped_wlock(){cond_.unlock_write();}
            }
            ;

            以下是測試用例:
            int?top?=?0;
            int?stack[1000];
            read_write_mutex?rwc;
            mutex?io_mutex;

            void?read(int?val)
            {
            ????
            for(int?i?=?0;?i?<?20;?++i)
            ????
            {
            ????????Sleep(
            2);

            ????????scoped_rlock?rlock(rwc);
            ????????
            int?s?=?top;

            ????????
            {
            ????????????mutex::scoped_lock?
            lock(io_mutex);
            ????????????cout?
            <<?"read?begin?"?<<?val?<<?endl;
            ????????}


            ????????Sleep(
            10);

            ????????
            {
            ????????????mutex::scoped_lock?
            lock(io_mutex);
            ????????????cout?
            <<?"read?end?"?<<?val?<<?endl;
            ????????}


            ????????
            if(s?!=?top)
            ????????
            {
            ????????????cout?
            <<?"error!"<<endl;
            ????????}

            ????}

            }


            void?write(int?val)
            {
            ????
            for(int?i?=?0;?i?<?20;?++i)
            ????
            {
            ????????Sleep(
            2);

            ????????scoped_wlock?wlock(rwc);

            ????????
            {
            ????????????mutex::scoped_lock?
            lock(io_mutex);
            ????????????cout?
            <<?"write?begin?"?<<?val?<<?endl;
            ????????}


            ????????Sleep(
            5);

            ????????
            {
            ????????????mutex::scoped_lock?
            lock(io_mutex);
            ????????????cout?
            <<?"write?end?"?<<?val?<<?endl;
            ????????}

            ????}

            }


            int?_tmain(int?argc,?_TCHAR*?argv[])
            {
            ????thread_group?rtg;

            ????
            for(int?i?=?0;?i?<?10;?++i)
            ????
            {
            ????????rtg.create_thread(boost::bind(
            &read,?i));
            ????}


            ????
            for(int?i?=?0;?i?<?2;?++i)
            ????
            {
            ????????rtg.create_thread(boost::bind(
            &write,?i));
            ????}


            ????rtg.join_all();

            ????system(
            "pause");

            ????
            return?0;
            }

            別忘了頭文件中:
            #include?<boost/thread.hpp>
            #include?
            <boost/bind.hpp>
            #include?
            <windows.h>
            #include?
            <iostream>

            using?namespace?std;
            using?namespace?boost;
            posted on 2006-10-23 19:38 空明流轉(zhuǎn) 閱讀(1814) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久亚洲日韩看片无码| 久久99精品国产99久久| 久久国产成人亚洲精品影院| 国产精品美女久久久久av爽| 一本色道久久综合| 久久w5ww成w人免费| 久久天天躁狠狠躁夜夜2020老熟妇 | 中文字幕久久精品| 日本强好片久久久久久AAA| 欧美亚洲国产精品久久蜜芽 | 日本人妻丰满熟妇久久久久久| 久久精品国产秦先生| 亚洲精品无码久久久久AV麻豆| 国产产无码乱码精品久久鸭| 久久久久亚洲?V成人无码| 久久精品国产亚洲AV电影| 亚洲国产香蕉人人爽成AV片久久| 国产精品久久久久久久久| 国产亚洲精品久久久久秋霞| 久久亚洲欧洲国产综合| 99久久精品免费看国产| 97久久久精品综合88久久| 久久人人爽人人爽人人爽| 久久精品国产精品亜洲毛片| 久久综合九色综合久99| av无码久久久久不卡免费网站| 亚洲人成无码www久久久| 精品人妻伦九区久久AAA片69 | 三级韩国一区久久二区综合| 国产99久久九九精品无码| 久久这里只有精品久久| 久久中文字幕一区二区| 成人妇女免费播放久久久| 色偷偷88888欧美精品久久久| 99久久免费国产精品特黄| 亚洲色欲久久久久综合网| 久久中文精品无码中文字幕| 欧美成a人片免费看久久| 一本综合久久国产二区| 久久久高清免费视频| 亚洲伊人久久成综合人影院|