自行实现线程池时候写的测试用例
为啥要自己实现?只是不要依赖,顺便方便放到内核的时候也用它罢了
magic命名空间是自己实现的一些通用sdk的实现,编程中逐渐不走sdk,后面甚至二进制也不会走系统的的二进制公共库。
多的不想放,放个线程池的测试代码+个实际使用的代码
namespace TestSamples
{
#ifndef MAGICTESTSAMPLESSIGN
#define MAGICTESTSAMPLESSIGN
#define TestFunLine printf("\r\n=========================%s=========================\r\n", __FUNCTION__);
//线程池测试代码
namespace threadpoolhelper
{
class MyClassY
{
public:
virtual void test() = 0;
};
class MyClassB :MyClassY
{
public:
virtual void test()
{
printf("1");
}
};
template<class T = MyClassY>
class MyClassN :T
{
public:
void hhh()
{
this->test();
}
};
//线程池接口,自己传入自己。
class CMyWorker :
public magic::IThreadPoolWorker<MyClassN<MyClassB>>
{
public:
//请求类指针,
// typedef typename MyClassN<MyClassB>* RequestType;
virtual bool Initialize(void* pvWorkerParam)
{
return 1;
}
virtual void Execute(typename RequestType request, void* pvWorkerParam, OVERLAPPED* pOverlapped)
{
request->hhh();
Sleep(0);
delete request;
}
virtual void Terminate(void* pvWorkerParam)
{
}
};
}
namespace __TestThreadPool {
class CThreadPoolTest
{
public:
CThreadPoolTest()
{
}
~CThreadPoolTest()
{
}
//线程池测试代码
void ThreadPoolTestFun()
{
TestFunLine;
magic::CThreadPool<threadpoolhelper::CMyWorker> pool;
pool.Initialize(0, -2);
for (size_t i = 0; i < 10000; i++)
{
pool.QueueRequest(new threadpoolhelper::MyClassN<threadpoolhelper::MyClassB>);
}
pool.ShutDown();
threadpoolhelper::MyClassN<threadpoolhelper::MyClassB> s;
s.hhh();
TestFunLine;
}
private:
};
}
#endif // !MAGICTESTSAMPLESSIGN
}
实例中使用
一个需要并发的场景