深度兼容std的vector,支持多线程同时读写
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
/* A multithread_safe vector TB */ #pragma once #include <vector> template<typename T> class CVector { private: std::vector<T> m_vecCon; CCriticalSection m_syncFlag; public: typedef typename std::vector<T>::size_type size_type; //typedef T value_type; typedef typename std::vector<T>::value_type value_type; typedef value_type& reference; //typedef const value_type& const_reference; typedef typename std::vector<T>::const_reference const_reference; //typedef value_type* iterator; typedef typename std::vector<T>::iterator iterator; //typedef const value_type* const_iterator; typedef typename std::vector<T>::const_iterator const_iterator; CVector(){} CVector(const CVector& cc) {*this = cc;} virtual ~CVector(void) {m_vecCon.clear();} CVector& operator=(const CVector& con) { m_syncFlag.Lock(); m_vecCon = con.m_vecCon; m_syncFlag.Unlock(); return *this; } CVector& operator=(const std::vector<T>& item) { m_syncFlag.Lock(); m_vecCon = item; m_syncFlag.Unlock(); return *this; } reference operator[](size_type n) { m_syncFlag.Lock(); reference ref = m_vecCon[n]; m_syncFlag.Unlock(); return ref; } const_reference operator[](size_type n)const { m_syncFlag.Lock(); const_reference ref = m_vecCon[n]; m_syncFlag.Unlock(); return ref; } operator std::vector<T>() const {return m_vecCon;} public: void push_back(const T& x) { m_syncFlag.Lock(); m_vecCon.push_back(x); m_syncFlag.Unlock(); } iterator insert(iterator pos, const T& x) { m_syncFlag.Lock(); m_vecCon.insert(pos, x); m_syncFlag.Unlock(); } bool empty() const {return m_vecCon.empty();} void reserve(size_type n) { m_syncFlag.Lock(); m_vecCon.reserve(n); m_syncFlag.Unlock(); } void pop_back() { m_syncFlag.Lock(); m_vecCon.pop_back(); m_syncFlag.Unlock(); } void swap(CVector& cc) { m_syncFlag.Lock(); m_vecCon.swap(cc.m_vecCon); m_syncFlag.Unlock(); } void swap(std::vector<T>& item) { m_syncFlag.Lock(); m_vecCon.swap(item); m_syncFlag.Unlock(); } reference at(size_type _pos) { m_syncFlag.Lock(); reference ref = m_vecCon.at(_pos); m_syncFlag.Unlock(); return ref; } const_reference at(size_type _pos) const { m_syncFlag.Lock(); const_reference ref = m_vecCon.at(_pos); m_syncFlag.Unlock(); return ref; } iterator erase(const_iterator _pos) { m_syncFlag.Lock(); iterator it = m_vecCon.erase(_pos); m_syncFlag.Unlock(); return it; } iterator erase(size_type _pos) { m_syncFlag.Lock(); iterator it = m_vecCon.erase(m_vecCon.begin()+_pos); m_syncFlag.Unlock(); return it; } iterator erase(const_iterator _first, const_iterator _last) { m_syncFlag.Lock(); iterator it = m_vecCon.erase(_first, _last); m_syncFlag.Unlock(); return it; } void assign(size_type _n, const T& _val) { m_syncFlag.Lock(); m_vecCon.assign(_n, _val); m_syncFlag.Unlock(); } void assign(iterator _first, iterator _last) { m_syncFlag.Lock(); m_vecCon.assign(_first, _last); m_syncFlag.Unlock(); } void clear() { m_syncFlag.Lock(); m_vecCon.clear(); m_syncFlag.Unlock(); } reference front() { m_syncFlag.Lock(); reference ref = *m_vecCon.begin(); m_syncFlag.Unlock(); return ref; } const_reference front() const { m_syncFlag.Lock(); const_reference ref = m_vecCon.front(); m_syncFlag.Unlock(); return ref; } reference back() { m_syncFlag.Lock(); reference ref = m_vecCon.back(); m_syncFlag.Unlock(); return ref; } const_reference back() const { m_syncFlag.Lock(); const_reference ref = m_vecCon.back(); m_syncFlag.Unlock(); return ref; } //iterator begin() {return m_vecCon.empty() ? NULL : &*m_vecCon.begin();} iterator begin() {return m_vecCon.begin();} const_iterator begin() const {return m_vecCon.begin();} iterator end() {return m_vecCon.end();} const_iterator end() const {return m_vecCon.end();} size_type size() const {return m_vecCon.size();} size_type capacity() const {return m_vecCon.capacity();} }; |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=578