• <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
            沒有什么實(shí)際意義(boost.threads已經(jīng)加入了讀寫鎖好像),只是說明一下condition的用法。
            鎖由Mutex和Lock構(gòu)成。
            Mutex是由condition實(shí)作而成的,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) 閱讀(1802) 評論(0)  編輯 收藏 引用

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


            99久久精品免费国产大片| 7国产欧美日韩综合天堂中文久久久久| 久久艹国产| 日韩久久无码免费毛片软件| 亚洲一区精品伊人久久伊人| 老色鬼久久亚洲AV综合| 亚洲国产精品久久| 欧美亚洲国产精品久久| 久久99精品久久久久子伦| 久久久久18| 精品无码久久久久久尤物| 99久久国产亚洲高清观看2024 | 欧美精品久久久久久久自慰| 国产精品一区二区久久| 尹人香蕉久久99天天拍| 99久久无码一区人妻a黑| 一级a性色生活片久久无少妇一级婬片免费放| 囯产精品久久久久久久久蜜桃| 91精品国产91久久| 久久久久AV综合网成人| 久久久久亚洲av成人网人人软件 | 欧美久久精品一级c片片| 中文字幕精品久久| 久久国产成人午夜AV影院| 99久久99这里只有免费的精品| 久久笫一福利免费导航| 久久国产香蕉视频| 99久久成人18免费网站| 99久久精品免费看国产一区二区三区| 亚洲精品美女久久久久99| 亚洲精品国产自在久久| 久久人搡人人玩人妻精品首页| 狠色狠色狠狠色综合久久| 久久精品国产99久久无毒不卡| 亚洲成av人片不卡无码久久 | 国产精品乱码久久久久久软件| 久久九九青青国产精品| 久久99国产精品久久久| 久久99国产精品99久久| 99久久99久久精品国产| 看全色黄大色大片免费久久久 |