#include #include #include #include #include using namespace std; #include "Thread.h" #include "Semaphore.h" #include "Mutex.h" class CFoo : public CThread { public: CFoo( CMutex *p ) : pMutex( p ) {}; void run() { while( -1 ) { pMutex->Lock(); } }; protected: CMutex *pMutex; }; int main() { int Step = 400; int i; int j; time_t t1; CMutex mutex; vector< CFoo* > vFoo; mutex.Initialize(); for ( i = 1; i <= 8; i++ ) { // スレッドを追加・生成 for ( j = 0; j < Step; j++ ) { CFoo *p = new CFoo( &mutex ); if ( !p->start() ) { printf( "Error : Create thread %d\n", vFoo.size() ); return 0; } else vFoo.push_back( p ); } // セマフォの解放を行う t1 = time( NULL ); for ( j = 0; j < 40000000; j++ ) { mutex.Unlock(); } printf( "Thread Count %d : time = %d\n", vFoo.size(), time( NULL ) - t1 ); } mutex.Destroy(); return 0; }