boost库在工作(10)引用计数的智能指针shared_array
在共享指针里,有时也需要共享一个缓冲区,比如从网络收到数据,需要先放到缓冲区里,接着把这些数据发送给处理线程进行解包和处理。在这里就需要使用到两个线程里共享缓冲区的问题,因此比较好的解决方案,就采用引用计数的智能指针shared_array来解决,最为合适不过了。shared_array与shared_ptr具有相同的形式,使用和注意点也是相似的。具体的使用的例子如下:
// boost_005.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <memory.h>#include <iostream>#include <boost/shared_array.hpp>//使用shared_array//软件开发人员: 蔡军生 2013-02-09void TestSharedArray(void){//一般指针操作const int nBufSize = 1024;boost::shared_array< char > pFirst(new char[nBufSize]);memset(pFirst.get(), 0x35, nBufSize);std::cout << pFirst[0] << std::endl;}int _tmain(int argc, _TCHAR* argv[]){TestSharedArray();system("PAUSE");return 0;}