• <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)  編輯 收藏 引用
            日本欧美国产精品第一页久久| 久久亚洲中文字幕精品一区四| 欧洲人妻丰满av无码久久不卡| 亚洲中文字幕无码一久久区| 色欲久久久天天天综合网| 久久精品国产第一区二区三区| 久久久久国产一级毛片高清版| 久久久久久久综合综合狠狠| 国产精品久久久久久五月尺| 久久久久久久久久久久中文字幕 | 久久精品一区二区影院| 久久国产亚洲精品| 亚洲成色999久久网站| 精品熟女少妇AV免费久久| 亚洲国产精品久久66| 久久精品国产亚洲AV蜜臀色欲| 国内精品久久久久影院免费| 理论片午午伦夜理片久久 | 国产69精品久久久久观看软件| 久久国产亚洲精品麻豆| 国产成人精品三上悠亚久久| 狠狠色综合网站久久久久久久 | 久久精品国产91久久综合麻豆自制| 伊人久久国产免费观看视频| 国产精品99久久久久久宅男| 狠狠色婷婷久久一区二区三区| 狠狠色婷婷久久一区二区 | 久久久久亚洲AV无码麻豆| 欧美性猛交xxxx免费看久久久| 国产精品久久影院| 9久久9久久精品| 久久免费的精品国产V∧| 亚洲AV乱码久久精品蜜桃| 久久精品国产日本波多野结衣| 一本色道久久综合狠狠躁篇 | 亚洲精品国产第一综合99久久| 久久AAAA片一区二区| 久久99精品国产麻豆婷婷| 国产精品美女久久久免费| 亚洲一区二区三区日本久久九| 99久久成人18免费网站|