// Tests for 'tbb_thread' class /* #ifdef _RTDEBUG // Case 1 #pragma comment( linker, "/heap:536870912,536870912" ) #pragma comment( linker, "/stack:4194304,4194304" ) // Case 2 // #pragma comment( linker, "/heap:268435456,268435456" ) // #pragma comment( linker, "/stack:1048576,1048576" ) #endif */ /* #ifdef _RTRELEASE // Case 1 #pragma comment( linker, "/heap:536870912,536870912" ) #pragma comment( linker, "/stack:4194304,4194304" ) // Case 2 // #pragma comment( linker, "/heap:268435456,268435456" ) // #pragma comment( linker, "/stack:1048576,1048576" ) #endif */ #include "tbb/tbb.h" #include "tbb/tbb_thread.h" using namespace std; using namespace tbb; void ExecuteThread( RTint iParam ); void ExecuteThread( RTint iParam ) { while( RTtrue ) { CrtPrintf( RTU("ExecuteThread function - Param value: %ld\n"), iParam ); ::Sleep( 500 ); } } class CThreadAction { public: CThreadAction() { m_iParam = 0; }; CThreadAction( RTint iParam ) { m_iParam = iParam; }; RTvoid operator()() { while( RTtrue ) { CrtPrintf( RTU("CThreadAction::operator() - Param Value: %ld\n"), m_iParam ); ::Sleep( 500 ); } }; RTint m_iParam; }; class CThreadParams { public: CThreadParams() { m_iThreadNum = 0; }; CThreadParams( RTint iThreadNum ) { m_iThreadNum = iThreadNum; }; RTvoid operator()() { while( RTtrue ) { CrtPrintf( RTU("CThreadParams::operator() - Param Value: %ld\n"), m_iThreadNum ); ::Sleep( 500 ); } }; RTint m_iThreadNum; }; class CParallelAlgorithmThreadsImpl { public: CParallelAlgorithmThreadsImpl( RTint iNumThreads ) { m_pptThreads = RTnull; m_iNumThreads = iNumThreads; }; virtual ~CParallelAlgorithmThreadsImpl() { if( m_pptThreads == RTnull ) return; if( m_iNumThreads == 0 ) return; for( RTint i = 0; i < m_iNumThreads; i++ ) { CrtDelete( m_pptThreads[i] ); m_pptThreads[i] = RTnull; } CrtFree( m_pptThreads ); m_pptThreads = RTnull; }; void Execute(); void ThreadExecute( const CThreadParams &Params ); tbb_thread **m_pptThreads; RTint m_iNumThreads; }; RTvoid CParallelAlgorithmThreadsImpl::Execute() { if( m_iNumThreads == 0 ) return; RTint i; m_pptThreads = ( tbb_thread ** )CrtCalloc( m_iNumThreads, sizeof( tbb_thread * ) ); CThreadParams Params1; CThreadParams Params2( m_iNumThreads ); CThreadAction Action1; for( i = 0; i < m_iNumThreads; i++ ) { // Test 01 - Status: Compilation Error // m_pptThreads[i] = CrtNew tbb::tbb_thread( &CParallelAlgorithmThreadsImpl::ThreadExecute, &(*this), Params ); // m_pptThreads[i] = CrtNew tbb::tbb_thread(); // Test 02 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( CThreadParams() ); // Test 03 - Status: Compiled \ Tested \ Works m_pptThreads[i] = CrtNew tbb::tbb_thread( CThreadParams( 1+i ) ); // Test 04 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( CThreadParams( 777+i ) ); // Test 04 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( Params1 ); // Test 05 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( Params2 ); // Test 06 - Status: Compiled \ Tested \ Works // CThreadParams Params3( 777+i ); // m_pptThreads[i] = CrtNew tbb::tbb_thread( Params3 ); // Test 07 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( CThreadAction() ); // Test 08 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( CThreadAction( 777+i ) ); // Test 09 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( Action1 ); // Test 10 - Status: Compiled \ Tested \ Works // CThreadAction Action2( 777+i ); // m_pptThreads[i] = CrtNew tbb::tbb_thread( Action2 ); // Test 11 - Status: Compiled \ Tested \ Works // m_pptThreads[i] = CrtNew tbb::tbb_thread( ExecuteThread, 777+i ); // Test 12 - Status: Compiled \ Tested \ Works } for( i = 0; i < m_iNumThreads; i++ ) { tbb_thread *pT = m_pptThreads[i]; if( pT == RTnull ) continue; if( pT->joinable() ) pT->join(); } } RTvoid CParallelAlgorithmThreadsImpl::ThreadExecute( const CThreadParams &Params ) { } RTvoid main( RTvoid ) { // CParallelAlgorithmThreadsImpl pat( 256 ); // DEBUG - It is possible to create more than 900 threads // CParallelAlgorithmThreadsImpl pat( 512 ); // RELEASE - It is possible to create more than 1000 threads CParallelAlgorithmThreadsImpl pat( 1024 ); pat.Execute(); }