• <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) 閱讀(1808) 評論(0)  編輯 收藏 引用
            久久精品极品盛宴观看| 国产精品久久久久久福利漫画 | 国产精品岛国久久久久| 精品久久777| 精品久久久久久无码中文野结衣| 国产精品综合久久第一页| 性做久久久久久久久浪潮| 97久久香蕉国产线看观看| 精品久久久久久无码中文字幕| 久久久这里只有精品加勒比| 国产亚洲精品美女久久久| 久久久久久青草大香综合精品| 欧美精品久久久久久久自慰| 国产高潮国产高潮久久久91| 99久久精品国产一区二区 | 人人狠狠综合久久亚洲婷婷| 7国产欧美日韩综合天堂中文久久久久| 色天使久久综合网天天| 88久久精品无码一区二区毛片 | 热99re久久国超精品首页| 久久国产欧美日韩精品| 国产精品欧美久久久久无广告| 久久国产精品99国产精| 97久久国产亚洲精品超碰热| 久久亚洲高清观看| 久久精品国产欧美日韩| 久久这里只有精品视频99| 久久精品无码午夜福利理论片| 精品熟女少妇av免费久久| 精品久久久噜噜噜久久久| 久久99精品国产99久久6| 欧美日韩久久中文字幕| 成人国内精品久久久久影院| 久久无码人妻精品一区二区三区 | 欧美牲交A欧牲交aⅴ久久| 久久成人影院精品777| 狠狠色噜噜色狠狠狠综合久久| 蜜桃麻豆www久久| 久久综合狠狠色综合伊人| 久久亚洲精品无码VA大香大香 | 久久精品免费一区二区|