3 #include "cppunit/thread.h"
4 #include <boost/date_time/posix_time/posix_time_types.hpp>
6 namespace CppUnitMini {
8 void TestThread::threadEntryPoint( TestThread * pInst )
13 void TestThread::create()
15 m_pThread = new boost::thread( threadEntryPoint, this );
18 void TestThread::run()
22 m_Pool.onThreadInitDone( this );
25 m_Pool.onThreadTestDone( this );
28 m_Pool.onThreadFiniDone( this );
30 catch ( std::exception& ex )
32 m_Pool.m_Test.message( "EXCEPTION in working thread: ");
33 m_Pool.m_Test.message( ex.what() );
37 void TestThread::error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line)
39 m_Pool.m_Test.error( in_macroName, in_macro, in_file, in_line );
43 ThreadPool::~ThreadPool()
45 delete m_pBarrierStart;
46 delete m_pBarrierDone;
48 for ( size_t i = 0; i < m_arrThreads.size(); ++i )
49 delete m_arrThreads[i];
50 m_arrThreads.resize( 0 );
53 void ThreadPool::add( TestThread * pThread, size_t nCount )
55 pThread->m_nThreadNo = m_arrThreads.size();
56 m_arrThreads.push_back( pThread );
58 TestThread * p = pThread->clone();
60 p->m_nThreadNo = m_arrThreads.size();
61 m_arrThreads.push_back( p );
66 void ThreadPool::run()
68 const size_t nThreadCount = m_arrThreads.size();
69 m_pBarrierStart = new boost::barrier( (unsigned int) nThreadCount );
70 // nThreadCount threads + current thread
71 m_pBarrierDone = new boost::barrier( (unsigned int) (nThreadCount + 1) );
73 for ( size_t i = 0; i < nThreadCount; ++i )
74 m_arrThreads[i]->create();
76 // Wait while all threads is done
77 m_pBarrierDone->wait();
78 boost::this_thread::sleep(boost::posix_time::milliseconds(500));
81 void ThreadPool::run( unsigned int nDuration )
83 const size_t nThreadCount = m_arrThreads.size();
84 m_pBarrierStart = new boost::barrier( (unsigned int) nThreadCount );
85 m_pBarrierDone = new boost::barrier( (unsigned int) (nThreadCount + 1) );
87 for ( size_t i = 0; i < nThreadCount; ++i )
88 m_arrThreads[i]->create();
90 boost::system_time stEnd( boost::get_system_time() + boost::posix_time::seconds( nDuration ) );
92 boost::this_thread::sleep( stEnd );
93 } while ( boost::get_system_time() < stEnd );
95 for ( size_t i = 0; i < nThreadCount; ++i )
96 m_arrThreads[i]->stop();
98 // Wait while all threads is done
99 m_pBarrierDone->wait();
100 boost::this_thread::sleep(boost::posix_time::milliseconds(500));
103 void ThreadPool::onThreadInitDone( TestThread * pThread )
105 // Calls in context of caller thread
106 // Wait while all threads started
107 m_pBarrierStart->wait();
109 pThread->m_Timer.reset();
112 void ThreadPool::onThreadTestDone( TestThread * pThread )
114 // Calls in context of caller thread
115 pThread->m_nDuration = pThread->m_Timer.duration();
118 void ThreadPool::onThreadFiniDone( TestThread * /*pThread*/ )
120 // Calls in context of caller thread
121 // Wait while all threads done
122 m_pBarrierDone->wait();