在使用线程处理时,必须确保非任务对象在任务需要它们的时候长期保留在活动状态,如果在任务完成之前,那些被任务使用的非任务对象已经被销毁,此时会导致非法访问存储单元。因此,那些被共享的非任务对象总要在堆中new出来,并且确保没有其他任务对象引用它时才动态销毁,此时就是引用计数。
ZThread库有一个引用计数的模板CountedPtr<>,它自动执行引用计数(执行拷贝构造函数时),并在引用计数归0时delete这个对象。每当有对象有被多于一个任务使用时,几乎总是需要使用CountedPtr<>来管理那些对象,以防由对角生存期争端而产生的问题。
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include "zthread/Thread.h"
- #include "zthread/Runnable.h"
- #include "zthread/PoolExecutor.h"
- using namespace ZThread;
- using namespace std;
- class CCount
- {
- public:
- void increment()
- {
- for (int i = 0; i < 100; i ++)
- {
- n[i]++;
- }
- //n++;
- }
- private:
- int n[100];
- //int n;
- };
- class Incrementer : public Runnable
- {
- public:
- //Incrementer(CCount* pC) : pCount(pC){}
- Incrementer(const CountedPtr<CCount>& pC) : pCount(pC){}
- void run()
- {
- for (int i = 100; i > 0; i--)
- {
- Thread::sleep(100);
- pCount->increment();
- cout << i << endl;
- }
- }
- private:
- //CCount* pCount;
- CountedPtr<CCount> pCount;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- //CCount count;
- CountedPtr<CCount> pCount(new CCount);
- try
- {
- //Thread t0(new Incrementer(&count));
- //Thread t1(new Incrementer(&count));
- Thread t0(new Incrementer(pCount));
- Thread t1(new Incrementer(pCount));
- }
- catch (Synchronization_Exception& e)
- {
- cerr << e.what() <<endl;
- }
- return 0;
- }
代码中注释起来的在栈中定义的CCount对象,会出现非法访问的错误。
而由CountedPtr<>模板管理的CCount对象,在任务两次对它引用时都会增1,只要任务运行,计数就会非0,当使用CCount对象的所有任务全部完成时(此时引用计数归0),才会delete CCount对象。