|
#include "stdafx.h"
class _TestTrace
{
_bstr_t m_bstrName;
public:
_TestTrace(_bstr_t bstrName):m_bstrName(bstrName)
{
printf("\n********** begin: %s **********\n",(LPCSTR)m_bstrName);
}
~_TestTrace()
{
printf("********** end: %s **********\n\n",(LPCSTR)m_bstrName);
}
};
#ifndef _TT
#define _TT _TestTrace _tt(__FUNCTION__);
#endif
void TestWaitThreadWithMessageLoop()
{
_TT
mb_thread_lib::CSafeThread<CMyThreadWithMsgLoop, _bstr_t> Thread;
Thread.Start( mb_thread_lib::smart_ptr<_bstr_t>(new _bstr_t("TestWaitThreadWithMessageLoop")) );
Thread.Wait();
}
void TestWaitThreadWithOutMessageLoop()
{
_TT
mb_thread_lib::CSafeThread<CMyThreadWithOutMsgLoop, _bstr_t> Thread;
Thread.Start( mb_thread_lib::smart_ptr<_bstr_t>(new _bstr_t("TestWaitThreadWithOutMessageLoop")) );
Thread.Wait();
}
void TestDeleteThreadWithMessageLoop()
{
_TT
mb_thread_lib::CSafeThread<CMyThreadWithMsgLoop, _bstr_t> Thread;
Thread.Start( mb_thread_lib::smart_ptr<_bstr_t>(new _bstr_t("TestWaitThreadWithMessageLoop")) );
Sleep(MY_SLEEP_TIME);//wait for a while
//will notify the thread to end in destructor
}
void TestDeleteThreadWithOutMessageLoop()
{
_TT
mb_thread_lib::CSafeThread<CMyThreadWithOutMsgLoop, _bstr_t> Thread;
Thread.Start( mb_thread_lib::smart_ptr<_bstr_t>(new _bstr_t("TestDeleteThreadWithOutMessageLoop")) );
Sleep(MY_SLEEP_TIME);//wait for a while
//will notify the thread to end in destructor
}
void TestWaitThreadWithOutMsgLoopWithEventObject()
{
_TT
mb_thread_lib::CSafeThread<CMyThreadWithOutMsgLoopWithEventObject, _bstr_t> Thread;
Thread.Start( mb_thread_lib::smart_ptr<_bstr_t>(new _bstr_t("WaitThreadWithOutMsgLoopWithEventObject")) );
for(int i = 0; i < 3; ++i)
{
//notify thread to do something
::SetEvent(Thread.m_hMyEventObject);
Sleep(MY_SLEEP_TIME);//wait for a while
}
//will notify the thread to end in destructor
}
void TestDeleteThreadWithOutMsgLoopWithEventObject()
{
_TT
mb_thread_lib::CSafeThread<CMyThreadWithOutMsgLoopWithEventObject, _bstr_t> Thread;
Thread.Start( mb_thread_lib::smart_ptr<_bstr_t>(new _bstr_t("TestDeleteThreadWithOutMsgLoopWithEventObject")) );
Sleep(MY_SLEEP_TIME);//wait for a while
//will notify the thread to end in destructor
}
void TestSequentialObjectPool()
{
_TT
//create sequental object pool (work queue)
mb_thread_lib::smart_ptr<mb_thread_lib::CSafeThread<mb_thread_lib::CSequentialObjectPool<_bstr_t>, _bstr_t> > spWorkQueue(new mb_thread_lib::CSafeThread<mb_thread_lib::CSequentialObjectPool<_bstr_t>, _bstr_t>());
//create a push thread (this thread feeds work queue with objects for sequental processing)
mb_thread_lib::CSafeThread<CPushThread, mb_thread_lib::CSafeThread<mb_thread_lib::CSequentialObjectPool<_bstr_t>, _bstr_t> > PushThread;
//start work queue
spWorkQueue->Start(mb_thread_lib::smart_ptr<_bstr_t>(NULL));
//start push thread
PushThread.Start(spWorkQueue);
Sleep(1000);
}
void TestThreadPool()
{
_TT
{
//create thread pool
mb_thread_lib::CThreadPool ThreadPool;
//initialize thread pool with 2 MIN threads, 15 MAX threads and thread idle time 10 sec
ThreadPool.InitPool(2, 15, 10000);
_variant_t var;
int i;
//creaet 50000 work items
for(i = 0; i < 50000; ++i)
{
var = i;
var.ChangeType(VT_BSTR);
CTestWorkItem* p = new CTestWorkItem();
p->m_bstr = var.bstrVal;
mb_thread_lib::smart_ptr<mb_thread_lib::CWorkItemInterface> spWorkItem(p);
//push work items into the thread pool
ThreadPool.QueueWorkItem(spWorkItem);
}
//push additional 50 work items
Sleep(100);
for(i = 0; i < 50; ++i)
{
var = i;
var.ChangeType(VT_BSTR);
CTestWorkItem* p = new CTestWorkItem();
p->m_bstr = var.bstrVal;
mb_thread_lib::smart_ptr<mb_thread_lib::CWorkItemInterface> spWorkItem(p);
ThreadPool.QueueWorkItem(spWorkItem);
}
Sleep(100);
//terminate the thread pool
ThreadPool.TermPool();
}
}
void TestTimerObject()
{
_TT
CTestWorkItem* p = new CTestWorkItem();
p->m_bstr = L"Timer work item.";
mb_thread_lib::smart_ptr<mb_thread_lib::CWorkItemInterface> spWorkItem(p);
mb_thread_lib::CTimer Timer;
DWORD d = 2000;
printf("execute handler every %d second(s)\n", d/1000);
Timer.Start(spWorkItem,d/* 2 sec. */);
Sleep(10000);
Timer.Stop();
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.