• <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>

            歲月流轉,往昔空明

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

            以下是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 空明流轉 閱讀(1808) 評論(0)  編輯 收藏 引用
            久久久无码精品亚洲日韩蜜臀浪潮| 日韩人妻无码一区二区三区久久| 国产99久久久国产精品~~牛| 久久99久久无码毛片一区二区| 亚洲伊人久久成综合人影院| 久久久久AV综合网成人| 久久无码国产| 久久香蕉一级毛片| 四虎国产精品成人免费久久| 72种姿势欧美久久久久大黄蕉 | 久久精品亚洲精品国产欧美| 伊人热热久久原色播放www| 国产精品99久久精品| 亚洲国产婷婷香蕉久久久久久| 精品午夜久久福利大片| 久久精品国产99国产精品亚洲| 久久精品女人天堂AV麻| 99精品久久精品一区二区| 中文字幕无码av激情不卡久久| 亚洲精品高清久久| 久久精品人人做人人爽电影蜜月| 亚洲国产香蕉人人爽成AV片久久 | 2021国产精品午夜久久| 国产国产成人久久精品| 久久超碰97人人做人人爱| 性高湖久久久久久久久AAAAA | 久久99久国产麻精品66| 久久久久国产精品麻豆AR影院| 99久久国语露脸精品国产| 亚洲精品无码久久久久| 亚洲AV无码久久寂寞少妇| 亚洲精品国产第一综合99久久| 国产午夜福利精品久久| 蜜桃麻豆www久久| 91性高湖久久久久| 狠狠人妻久久久久久综合蜜桃| 色综合久久中文色婷婷| 91亚洲国产成人久久精品网址| 亚洲国产精品久久久久网站| 麻豆精品久久精品色综合| 99久久国产亚洲高清观看2024|