From 705122e74cc531a304ab56a7a3200c9a359152e8 Mon Sep 17 00:00:00 2001 From: rich_sposato Date: Sat, 14 Oct 2006 00:06:15 +0000 Subject: [PATCH] Fixed a couple of bugs. Added lines to send test info to output. Added use of ScopeGuard. Removed superfluous code. git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@715 7ec92016-0320-0410-acc4-a06ded1c099a --- test/SmartPtr/LockTest.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/test/SmartPtr/LockTest.cpp b/test/SmartPtr/LockTest.cpp index 5576660..499bfc5 100644 --- a/test/SmartPtr/LockTest.cpp +++ b/test/SmartPtr/LockTest.cpp @@ -14,20 +14,21 @@ // ---------------------------------------------------------------------------- -//#define LOKI_CLASS_LEVEL_THREADING +/// @note This test uses LOKI_OBJECT_LEVEL_THREADING because StrongPtr's +/// LockableTwoRefCounts policy can't be used with a single-threaded model. +/// It requires either object-level-locking or class-level-locking. -#ifndef LOKI_CLASS_LEVEL_THREADING - #define LOKI_OBJECT_LEVEL_THREADING -#endif #if defined(_WIN32) #include #include + #define LOKI_WINDOWS_H #endif #include #include #include +#include #include #include @@ -112,7 +113,9 @@ public: { for( size_t ii = 0; ii < threadCount; ii++ ) { - Printf( "Creating thread %d\n" )( ii ); + string buffer; + SPrintf( buffer, "Creating thread %d\n" )( ii ); + cout << buffer; Thread * thread = new Thread( function, reinterpret_cast< void * >( ii ) ); m_threads.push_back( thread ); @@ -122,7 +125,12 @@ public: void Start( void ) { for ( size_t ii = 0; ii < m_threads.size(); ii++ ) + { + string buffer; + SPrintf( buffer, "Starting thread %d\n" )( ii ); + cout << buffer; m_threads.at( ii )->Start(); + } } void Join( void ) const @@ -155,7 +163,7 @@ public: A( void ) {} -#define BIG_FOR_LOOP for( unsigned int i = 0; i < 10000000; i++ ) g++; +#define BIG_FOR_LOOP for( unsigned int i = 0; i < 5000000; i++ ) g++; void Print( void * id ) const { @@ -207,7 +215,7 @@ public: static void Destroy( void ) { - if ( NULL == s_instance ) + if ( NULL != s_instance ) { delete s_instance; s_instance = NULL; @@ -242,7 +250,7 @@ public: static void Destroy( void ) { - if ( NULL == s_instance ) + if ( NULL != s_instance ) { delete s_instance; s_instance = NULL; @@ -271,8 +279,9 @@ void * RunLocked( void * id ) for( unsigned int i = 0; i < loop; i++ ) { ap.Lock(); + Loki::ScopeGuard unlockGuard = MakeGuard( &A_Lockable_ptr::Unlock, ap ); + (void)unlockGuard; ap->Print( id ); - ap.Unlock(); } return 0; } @@ -293,12 +302,6 @@ void * Run( void * id ) void DoLockedPtrTest( void ) { -#if defined(_MSC_VER) - /** @note For some reason, mutexes implemented using Microsoft's - CriticalSection don't work properly unless this mutex is here. - */ - Loki::Mutex mutex; - SafeA::GetIt(); UnsafeA::GetIt(); ::system( "pause" ); @@ -317,12 +320,15 @@ void DoLockedPtrTest( void ) } SafeA::Destroy(); UnsafeA::Destroy(); -#endif } // ---------------------------------------------------------------------------- // $Log$ +// Revision 1.3 2006/10/14 00:06:15 rich_sposato +// Fixed a couple of bugs. Added lines to send test info to output. Added +// use of ScopeGuard. Removed superfluous code. +// // Revision 1.2 2006/06/08 19:15:27 lfittl // - Simplify some threading code by not saving the return status // (also fixes 2 gcc warnings)