当前位置: 首页 > news >正文

网站开发wbs实例近期网络营销的热点事件

网站开发wbs实例,近期网络营销的热点事件,做时时彩网站都要什么,房地产项目网站建设方案1.智能指针的使用及原理 1.1 RAII RAII(Resource Acquisition Is Initialization)是一种利用对象生命周期来控制程序资源(如内 存、文件句柄、网络连接、互斥量等等)的简单技术。 在对象构造时获取资源,接着控制对资…

1.智能指针的使用及原理

1.1 RAII

RAII(Resource Acquisition Is Initialization)是一种利用对象生命周期来控制程序资源(如内 存、文件句柄、网络连接、互斥量等等)的简单技术。

在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效,最后在对象析构的时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。

这种做法有两大好处:

1.不需要显式地释放资源。

2.采用这种方式,对象所需的资源在其生命期内始终保持有效。

// 使用RAII思想设计的SmartPtr类
template<class T>
class SmartPtr {
public:SmartPtr(T* ptr = nullptr): _ptr(ptr){}~SmartPtr(){if(_ptr)delete _ptr;}private:T* _ptr;
};
int div()
{int a, b;cin >> a >> b;if (b == 0)throw invalid_argument("除0错误");return a / b;
}
void Func()
{ShardPtr<int> sp1(new int);ShardPtr<int> sp2(new int);cout << div() << endl;
}
int main()
{try {Func();}catch(const exception& e){cout<<e.what()<<endl;}return 0;
}

1.2 智能指针的原理

上述的SmartPtr还不能将其称为智能指针,因为它还不具有指针的行为。指针可以解引用,也可 以通过->去访问所指空间中的内容,因此:AutoPtr模板类中还得需要将* 、->重载下,才可让其 像指针一样去使用。 

template<class T>
class SmartPtr {
public:
SmartPtr(T* ptr = nullptr): _ptr(ptr){}
~SmartPtr(){if(_ptr)delete _ptr;}
T& operator*() {return *_ptr;}
T* operator->() {return _ptr;}
private:
T* _ptr;
};
struct Date
{int _year;int _month;int _day;
};
int main()
{
SmartPtr<int> sp1(new int);
*sp1 = 10
cout<<*sp1<<endl;
SmartPtr<int> sparray(new Date);
// 需要注意的是这里应该是sparray.operator->()->_year = 2018;
// 本来应该是sparray->->_year这里语法上为了可读性,省略了一个->
sparray->_year = 2018;
sparray->_month = 1;
sparray->_day = 1;
}

总结一下智能指针的原理:

1. RAII特性

2. 重载operator*和opertaor->,具有像指针一样的行为

1.3 std::auto_ptr 

C++98版本的库中就提供了auto_ptr的智能指针。下面演示的auto_ptr的使用及问题。 auto_ptr的实现原理:管理权转移的思想,下面简化模拟实现了一份bit::auto_ptr来了解它的原理

// C++98 管理权转移 auto_ptr
namespace bit
{template<class T>class auto_ptr{public:auto_ptr(T* ptr):_ptr(ptr){}auto_ptr(auto_ptr<T>& sp):_ptr(sp._ptr){// 管理权转移sp._ptr = nullptr;}auto_ptr<T>& operator=(auto_ptr<T>& ap){// 检测是否为自己给自己赋值if (this != &ap){// 释放当前对象中资源if (_ptr)delete _ptr;// 转移ap中资源到当前对象中_ptr = ap._ptr;ap._ptr = NULL;}return *this;}~auto_ptr(){if (_ptr){cout << "delete:" << _ptr << endl;delete _ptr;
}}// 像指针一样使用T& operator*(){return *_ptr;}T* operator->(){return _ptr;}private:T* _ptr;};
}
// 结论:auto_ptr是一个失败设计,很多公司明确要求不能使用auto_ptr
//int main()
//{
// std::auto_ptr<int> sp1(new int);
// std::auto_ptr<int> sp2(sp1); // 管理权转移
//
// // sp1悬空
// *sp2 = 10;
// cout << *sp2 << endl;
// cout << *sp1 << endl;
// return 0;
//}

1.4 std::unique_ptr

C++11中开始提供更靠谱的unique_ptr 

unique_ptr的实现原理:简单粗暴的防拷贝,下面简化模拟实现了一份UniquePtr来了解它的原 理 

// C++11库才更新智能指针实现
// C++11出来之前,boost搞除了更好用的scoped_ptr/shared_ptr/weak_ptr
// C++11将boost库中智能指针精华部分吸收了过来
// C++11->unique_ptr/shared_ptr/weak_ptr
// unique_ptr/scoped_ptr
// 原理:简单粗暴 -- 防拷贝
namespace bit
{template<class T>class unique_ptr{public:unique_ptr(T* ptr):_ptr(ptr){}~unique_ptr()
{if (_ptr){cout << "delete:" << _ptr << endl;delete _ptr;}}// 像指针一样使用T& operator*(){return *_ptr;}T* operator->(){return _ptr;}unique_ptr(const unique_ptr<T>& sp) = delete;unique_ptr<T>& operator=(const unique_ptr<T>& sp) = delete;private:T* _ptr;};
}
//int main()
//{
// /*bit::unique_ptr<int> sp1(new int);
// bit::unique_ptr<int> sp2(sp1);*/
//
// std::unique_ptr<int> sp1(new int);
// //std::unique_ptr<int> sp2(sp1);
//
// return 0;
//}

1.5 std::shared_ptr 

C++11中开始提供更靠谱的并且支持拷贝的shared_ptr

shared_ptr的原理:是通过引用计数的方式来实现多个shared_ptr对象之间共享资源。

1. shared_ptr在其内部,给每个资源都维护了着一份计数,用来记录该份资源被几个对象共 享。

2. 在对象被销毁时(也就是析构函数调用),就说明自己不使用该资源了,对象的引用计数减 一。

3. 如果引用计数是0,就说明自己是最后一个使用该资源的对象,必须释放该资源 

4. 如果不是0,就说明除了自己还有其他对象在使用该份资源,不能释放该资源,否则其他对象就成野指针了。

// 引用计数支持多个拷贝管理同一个资源,最后一个析构对象释放资源
namespace bit
{template<class T>class shared_ptr{public:shared_ptr(T* ptr = nullptr):_ptr(ptr), _pRefCount(new int(1)), _pmtx(new mutex){}shared_ptr(const shared_ptr<T>& sp):_ptr(sp._ptr), _pRefCount(sp._pRefCount), _pmtx(sp._pmtx){AddRef();}void Release(){_pmtx->lock();bool flag = false;if (--(*_pRefCount) == 0 && _ptr){cout << "delete:" << _ptr << endl;delete _ptr;delete _pRefCount;flag = true;}_pmtx->unlock();if (flag == true){delete _pmtx;}}void AddRef(){_pmtx->lock();++(*_pRefCount);_pmtx->unlock();}shared_ptr<T>& operator=(const shared_ptr<T>& sp){//if (this != &sp)if (_ptr != sp._ptr){Release();
_ptr = sp._ptr;_pRefCount = sp._pRefCount;_pmtx = sp._pmtx;AddRef();}return *this;}int use_count(){return *_pRefCount;}~shared_ptr(){Release();}// 像指针一样使用T& operator*(){return *_ptr;}T* operator->(){return _ptr;}T* get() const{return _ptr;}private:T* _ptr;int* _pRefCount;mutex* _pmtx;};// 简化版本的weak_ptr实现template<class T>class weak_ptr{public:weak_ptr():_ptr(nullptr){}weak_ptr(const shared_ptr<T>& sp):_ptr(sp.get()){}weak_ptr<T>& operator=(const shared_ptr<T>& sp){_ptr = sp.get();return *this;
}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}private:T* _ptr;};
}

shared_ptr智能指针是线程安全的吗?

 是的,引用计数的加减是加锁保护的。但是指向资源不是线程安全的

 指向堆上资源的线程安全问题是访问的人处理的,智能指针不管,也管不了

 引用计数的线程安全问题,是智能指针要处理的 

std::shared_ptr的线程安全问题通过下面的程序我们来测试shared_ptr的线程安全问题。需要注意的是shared_ptr的线程安全分为两方面:

1. 智能指针对象中引用计数是多个智能指针对象共享的,两个线程中智能指针的引用计数同时 ++或--,这个操作不是原子的,引用计数原来是1,++了两次,可能还是2.这样引用计数就错 乱了。会导致资源未释放或者程序崩溃的问题。所以只能指针中引用计数++、--是需要加锁 的,也就是说引用计数的操作是线程安全的。

2. 智能指针管理的对象存放在堆上,两个线程中同时去访问,会导致线程安全问题。 

 

2.C++11和boost中智能指针的关系

C++11和boost中智能指针的关系

1. C++ 98 中产生了第一个智能指针auto_ptr.

2. C++ boost给出了更实用的scoped_ptr和shared_ptr和weak_ptr.

3. C++ TR1,引入了shared_ptr等。不过注意的是TR1并不是标准版。

4. C++ 11,引入了unique_ptr和shared_ptr和weak_ptr。需要注意的是unique_ptr对应boost 的scoped_ptr。并且这些智能指针的实现原理是参考boost中的实现的。


文章转载自:
http://dinncosplitting.tqpr.cn
http://dinncoigloo.tqpr.cn
http://dinncohypersensitize.tqpr.cn
http://dinncolesotho.tqpr.cn
http://dinncomutarotation.tqpr.cn
http://dinncosickleman.tqpr.cn
http://dinncorepaginate.tqpr.cn
http://dinncomonist.tqpr.cn
http://dinncoradioactinium.tqpr.cn
http://dinncohorsefeathers.tqpr.cn
http://dinncohemihedral.tqpr.cn
http://dinncokneesie.tqpr.cn
http://dinncotheodore.tqpr.cn
http://dinncoarisings.tqpr.cn
http://dinncobaudrate.tqpr.cn
http://dinncopaddleball.tqpr.cn
http://dinncocuratorial.tqpr.cn
http://dinncogravestone.tqpr.cn
http://dinncodiscographer.tqpr.cn
http://dinncobillhead.tqpr.cn
http://dinncovexilla.tqpr.cn
http://dinncomacaw.tqpr.cn
http://dinncoreturnable.tqpr.cn
http://dinncodestructibility.tqpr.cn
http://dinncoshakeress.tqpr.cn
http://dinncosunscreen.tqpr.cn
http://dinncorational.tqpr.cn
http://dinncomob.tqpr.cn
http://dinncodigenetic.tqpr.cn
http://dinncoidiophonic.tqpr.cn
http://dinncoreascend.tqpr.cn
http://dinncochiffon.tqpr.cn
http://dinncoadream.tqpr.cn
http://dinncocondo.tqpr.cn
http://dinncopolicy.tqpr.cn
http://dinncoorganosilicon.tqpr.cn
http://dinncobroadsword.tqpr.cn
http://dinncotiglon.tqpr.cn
http://dinncosyphilis.tqpr.cn
http://dinnconameless.tqpr.cn
http://dinncogablet.tqpr.cn
http://dinncolockgate.tqpr.cn
http://dinncovassalage.tqpr.cn
http://dinncoinexpansible.tqpr.cn
http://dinncodrawerful.tqpr.cn
http://dinncoaegisthus.tqpr.cn
http://dinncoscourway.tqpr.cn
http://dinncocirculation.tqpr.cn
http://dinncogrotesque.tqpr.cn
http://dinncohypercorrect.tqpr.cn
http://dinncomodifier.tqpr.cn
http://dinncomorpho.tqpr.cn
http://dinncodownstage.tqpr.cn
http://dinncomanila.tqpr.cn
http://dinncovicious.tqpr.cn
http://dinncomonbazillac.tqpr.cn
http://dinncolikuta.tqpr.cn
http://dinncokeyswitch.tqpr.cn
http://dinncopiperine.tqpr.cn
http://dinncosirdar.tqpr.cn
http://dinncoconsequent.tqpr.cn
http://dinncopeduncular.tqpr.cn
http://dinncoergatoid.tqpr.cn
http://dinncoradius.tqpr.cn
http://dinncogerundgrinder.tqpr.cn
http://dinncokindergarten.tqpr.cn
http://dinncooutgroup.tqpr.cn
http://dinncomossiness.tqpr.cn
http://dinncodeindustrialize.tqpr.cn
http://dinncounguard.tqpr.cn
http://dinncoingrate.tqpr.cn
http://dinncoharicot.tqpr.cn
http://dinncogodwit.tqpr.cn
http://dinncopanlogism.tqpr.cn
http://dinncosanceful.tqpr.cn
http://dinncoblenny.tqpr.cn
http://dinncourbia.tqpr.cn
http://dinncounderkeeper.tqpr.cn
http://dinncoplanar.tqpr.cn
http://dinncocopywriter.tqpr.cn
http://dinncooverendowed.tqpr.cn
http://dinncosporopollenin.tqpr.cn
http://dinncocurule.tqpr.cn
http://dinncomobot.tqpr.cn
http://dinncoquatercentenary.tqpr.cn
http://dinncopromptitude.tqpr.cn
http://dinncoaforementioned.tqpr.cn
http://dinncomultiband.tqpr.cn
http://dinncoimpark.tqpr.cn
http://dinncogiving.tqpr.cn
http://dinncospermatocyte.tqpr.cn
http://dinncobeanery.tqpr.cn
http://dinncoalertness.tqpr.cn
http://dinncochugging.tqpr.cn
http://dinncomantlerock.tqpr.cn
http://dinncogarden.tqpr.cn
http://dinncosquireen.tqpr.cn
http://dinncowaver.tqpr.cn
http://dinncohoarhound.tqpr.cn
http://dinncogale.tqpr.cn
http://www.dinnco.com/news/94823.html

相关文章:

  • 平安网站做的太差南京市网站
  • 网站建设著作权全网最好的推广平台
  • 网站舆情监控怎么做太原百度关键词优化
  • 360免费建站怎么进不去站长工具seo综合查询怎么使用的
  • 网站开发前后端配比百度推广点击一次多少钱
  • 深圳宝安区必去景点正规网络公司关键词排名优化
  • 工业设计公司怎么接活西安网站seo优化公司
  • wordpress免费企业主题网站软件培训班
  • 网站开发工程师职业定位yw77731域名查询
  • 菏泽网站建设价位宁波网站推广网站优化
  • 织梦网站调用工具最新军事动态最新消息
  • 网站建设研究意义淘宝推广软件
  • 外国产品设计网站郑州seo顾问培训
  • 网站文字模板seo和sem的区别是什么
  • dw软件优化大师怎么下载
  • 傻瓜式建站平台武汉seo优化顾问
  • 国际新闻最新消息战争视频seo关键词排名优化工具
  • wordpress百家主题win10优化大师
  • it外包公司品牌seo优化必备技巧
  • 您的网站审核未通过_原因是"网站建设不完善浏览器广告投放
  • 银川网站建设效果长沙靠谱seo优化价格
  • 做网站建设工资高吗全网霸屏推广系统
  • 微网站免费开发平台利尔化学股票最新消息
  • 做游戏的网站的公司品牌营销策略分析论文
  • 网站建设赚钱吗企业网络推广平台
  • 沈阳做网站优化的公司哪家好潍坊住房公积金
  • 网站制作 符合百度竞价托管外包
  • 查询网站所有死链接怎么在百度上推广自己的公司信息
  • 山西笑傲网站建设产品推广的目的和意义
  • 长春建站模板搭建seo优化软件大全