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

            安全的list

                好久沒有寫東西了,實在是忙,其實,準確說是懶。

                最近調試客戶端地圖管理的時候,老是出現對象在視野中進進出出后就會冒出個非法。而非法的原因大多跟list有關,就是在list遍歷顯示列表的時候,出現了增刪操作,而這個是list不能容忍的。由于系統比較大,要改的地方也多,而且好多異常情況也不是一下子就能改好的,當屏幕中的生物對象很多時,就容易出現。

            想來想去,就想了一個能安全遍歷的list來解決問題。因為在實際上,當遍歷到一個list的節點時,會調用節點所含對象的update方法,該方法可能會觸發從地圖中刪除自己或者其他對象,這樣list就非法了。

            下面是對標準list的簡單封裝,使之具有安全遍歷的特性,遍歷過程中可以增刪任何節點。原理很簡單,就是內部記住遍歷的當前節點,在刪除時做個比較。

            //==========================================================================
            /**
            * @file      : safelist.h
            * @author : PeakGao <peakgao163@163.com>
            * created : 2008-11-13   20:21
            * purpose : safe list
            */

            //==========================================================================

            #ifndef __safelist_h__
            #define __safelist_h__

            #include 
            <list>

            /** 安全list
            對標準list進行了簡單封裝,使之具有安全遍歷的功能(即:在遍歷過程中,支持增刪節點)

            // 普通遍歷
            for (safelist<int>::const_iterator it = list.begin(); it!=list.end(); ++it)
            {
                Info("val = "<<*it<<endl);
            }

            // 安全遍歷(允許在遍歷的過程中增刪任何數目的節點)
            for (safelist<int>::iterator it=list.find_first(); it!=list.end(); it=list.find_next(it))
            {
                Info("val = "<<*it<<endl);
                if (*it == 3)
                {
                    list.erase(it);
                }
            }
            */

            template
            <class _Ty, class _Ax = std::allocator<_Ty> >
            class safelist : public std::list<_Ty, _Ax>
            {
            public:
                typedef typename std::list
            <_Ty, _Ax>    _Mybase;
                typedef typename safelist
            <_Ty, _Ax>        _Myt;
                typedef typename _Mybase::_Alloc        _Alloc;

            private:
                mutable _Nodeptr    _Cur;    
            /// the cursor for for_each

            public:
                safelist() : _Mybase(), _Cur(_Myhead) 
            { }
                
            explicit safelist(const _Alloc& _Al) : _Mybase(_Al), _Cur(_Myhead) { }
                
            explicit safelist(size_type _Count) : _Mybase(_Count), _Cur(_Myhead) { }
                safelist(size_type _Count, 
            const _Ty& _Val) : _Mybase(_Count, _Val), _Cur(_Myhead) { }
                safelist(size_type _Count, 
            const _Ty& _Val, const _Alloc& _Al) : _Mybase(_Count, _Val, _Al), _Cur(_Myhead) { }
                safelist(
            const _Mybase& _Right) : _Mybase(_Right), _Cur(_Myhead) { }
                safelist(
            const _Myt& _Right) : _Mybase(_Right), _Cur(_Myhead) { }
                template
            <class _Iter>
                safelist(_Iter _First, _Iter _Last) : _Mybase(_First, _Last), _Cur(_Myhead) 
            { }
                template
            <class _Iter>
                safelist(_Iter _First, _Iter _Last, 
            const _Alloc& _Al) : _Mybase(_First, _Last, _Al), _Cur(_Myhead) { }

                
            ~safelist()
                
            {
                    _Cur 
            = 0;
                }


                
            void clear()
                
            {
                    _Mybase::clear();
                    _Cur 
            = _Myhead;
                }


                iterator erase(iterator _Where)
                
            {
                    _Nodeptr cur 
            = _Where._Mynode();
                    
            if (_Cur == cur)
                        _Cur 
            = _Nextnode(cur);
                    
            return _Mybase::erase(_Where);
                }


                
            // 用于安全遍歷
            public:
                iterator find_first()                
            return iterator(_Cur = _Nextnode(_Myhead), this); }
                const_iterator find_first() 
            const    return const_iterator(_Cur = _Nextnode(_Myhead), this); }

                iterator find_next(iterator cur)
                
            {
                    
            if (cur._Mynode() == _Cur)
                        _Cur 
            = _Nextnode(_Cur);
                    
            return iterator(_Cur, this);
                }


                const_iterator find_next(const_iterator cur) 
            const
                
            {
                    
            if (cur._Mynode() == _Cur)
                        _Cur 
            = _Nextnode(_Cur);
                    
            return const_iterator(_Cur, this);
                }

            }
            ;

            posted on 2008-11-15 01:41 PeakGao 閱讀(3385) 評論(11)  編輯 收藏 引用 所屬分類: C++技術

            評論

            # re: 安全的list 2008-11-16 09:40 winsty

            這個不是線程安全的問題么……  回復  更多評論   

            # ????? 2008-11-16 18:45 是什么

            看不懂 是什么哦
            電腦程序
              回復  更多評論   

            # re: 安全的list[未登錄] 2008-11-17 10:30 PeakGao

            @是什么
            是這樣的,當list的操作很簡單時,遍歷list幾乎沒有什么問題,也可以在遍歷的時候刪除當前節點,如:
            for (std::list<int>::iterator it=list.begin(); it!=list.end();)
            {
            if (條件為真)
            it = list.erase(it); // 刪除當前節點
            else
            ++it;
            }

            但是當這個list不是很簡單的遍歷時,而且刪除的時候也不是很顯式的在遍歷過程中時,就很容易出問題,如:

            void MapManager::update(...)
            {
            // typedef std::list<Entity*> DisplayList;
            for (DisplayList::iterator it=mDisplayList.begin(); it!=mDisplayList.end();)
            {
            (*it)->update(...);
            }
            }

            但是(*it)->update(...);會調用到另一個模塊中去了,可能會這樣調用:
            void Entity::update(...)
            {
            //...
            MapManager->removeEntity(this);
            }

            而removeEntity會涉及到erase節點:
            void MapManager::removeEntity(Entity* e)
            {
            mDisplayList.remove(e);
            }

            如果Entity的update方法中,發現自己的生命期已經結束的話,就會刪除自己,這樣MapManager::update里面就非法了,這是一個站在磚頭上拿掉磚頭的問題,必定非法。這個safelist就是為了支持在遍歷列表的過程中能安全的erase任何節點。

            可能你們沒有碰到該類問題,或者使用list的時候沒有那么復雜,所以一時沒法去了解。  回復  更多評論   

            # re: 安全的list 2008-11-17 14:33 不懂

            std::list<int>::iterator itTmp;

            for (std::list<int>::iterator it=list.begin(); it!=list.end();)
            {
            if (條件為真)
            {
            itTmp = it;
            ++itTmp;
            it = list.erase(it); // 刪除當前節點
            it = itTmp;
            }
            else
            ++it;
            }
              回復  更多評論   

            # re: 安全的list 2008-11-18 08:38 不懂

            但是當這個list不是很簡單的遍歷時,而且刪除的時候也不是很顯式的在遍歷過程中時,就很容易出問題  回復  更多評論   

            # re: 安全的list 2008-11-18 08:38 不懂

            但是當這個list不是很簡單的遍歷時,而且刪除的時候也不是很顯式的在遍歷過程中時,就很容易出問題

            如果真有這樣的問題,那就是框架有問題  回復  更多評論   

            # re: 安全的list[未登錄] 2008-11-18 08:58 PeakGao

            @不懂
            理論上是這樣,框架徹底的好就沒有問題,但是在游戲更新時,經常有生命期結束的對象,這樣的對象需要從地圖上面移除,就涉及到從列表中erase,而生命期結束是根據update的調用進行檢測的。當然可以有另一個辦法,就是將檢測放到一個時鐘里面,而不是在list的遍歷過程中,但是這樣會需要好多多余的時鐘。再有一種辦法,就是對象要移除時,只設置一個需要移除的標志,在下一輪遍歷前才真正移除。發現越說越復雜了,總之,這個功能就是用于list遍歷很復雜時,也能安全的工作。你的這幾行,參考我上面的,就一句it=list.erase(it)迭代器不需要臨時保存的!!
            for (std::list<int>::iterator it=list.begin(); it!=list.end();)
            {
            if (條件為真)
            {
            itTmp = it; // 多余
            ++itTmp; // 多余
            it = list.erase(it); // 刪除當前節點
            it = itTmp; // 多余
            }
            else
            ++it;
            }   回復  更多評論   

            # re: 安全的list 2008-11-18 09:29 Jeff Chen

            LZ的情況,我也遇到過,困惑過。當我看完jabberd2的代碼后,覺得它的做法比較好。

            方法如下:
            程序每次先遍歷所有的Connection時,無效的Connection將自己移入一個CloseList中。
            在遍歷所有的Connection后,程序接著清理CloseList里的Connection。

            這樣做的好處,不會出現LZ這種list“重入”的問題,而且可以靈活處理不需要的對象。  回復  更多評論   

            # re: 安全的list 2008-11-18 17:06 LOGOS

            頂樓上
            mark it & lazy delete

            這樣做在在邏輯上更為完整,相對于作者直接刪除對象而言
              回復  更多評論   

            # re: 安全的list[未登錄] 2008-11-20 13:35 PeakGao

            @Jeff Chen
            你這種其實就是我上面說的這個意思:“再有一種辦法,就是對象要移除時,只設置一個需要移除的標志,在下一輪遍歷前才真正移除。”,不過你的說法好像有問題哦,遍歷時根本不知道是無效的Connection哦,而且不能在遍歷的過程中將節點移入到另一個列表,這樣會掛的  回復  更多評論   

            # re: 安全的list(更簡單的用法) 2009-01-08 08:46 canaan

            http://zhgn.vicp.net/boke/200901071529.htm
            List.erase(p)的用法  回復  更多評論   

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿(9)

            隨筆分類(67)

            隨筆檔案(65)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲欧美伊人久久综合一区二区 | 久久伊人精品青青草原高清| 亚洲日韩欧美一区久久久久我| 精品久久久久久久中文字幕| 性高朝久久久久久久久久| 色播久久人人爽人人爽人人片AV| 亚洲中文字幕无码久久2020| 久久精品国产亚洲麻豆| 久久精品卫校国产小美女| 99精品国产在热久久无毒不卡| 国产精品久久久天天影视香蕉| 国产aⅴ激情无码久久| AAA级久久久精品无码区| 性欧美大战久久久久久久久| 久久成人18免费网站| 精品无码久久久久国产| 日韩久久久久中文字幕人妻 | 久久免费香蕉视频| 69国产成人综合久久精品| 亚洲午夜无码久久久久小说| 99久久国产亚洲高清观看2024| 亚洲午夜无码久久久久| 久久精品成人免费观看97| 久久精品一区二区三区不卡| 久久婷婷五月综合国产尤物app| 国产精品欧美亚洲韩国日本久久 | 99国产精品久久| 欧美黑人又粗又大久久久| 久久久久亚洲AV无码专区首JN| 久久久WWW免费人成精品| 久久久久综合网久久| 久久久中文字幕| 久久综合久久综合九色| 亚洲一区二区三区日本久久九| 久久99热只有频精品8| 亚洲精品tv久久久久久久久| 久久人妻少妇嫩草AV蜜桃| 色综合久久综合中文综合网| 综合人妻久久一区二区精品| 久久强奷乱码老熟女网站| 亚洲AV乱码久久精品蜜桃|