#include #include #include #include #include using namespace std; #include "Thread.h" #include "Semaphore.h" class CFoo : public CThread { public: CFoo( CSemaphore *p ) : pSema( p ) {}; void run() { while( -1 ) { pSema->P(); } }; protected: CSemaphore *pSema; }; int main() { int Step = 125; int i; int j; time_t t1; CSemaphore sema( 0 ); vector< CFoo* > vFoo; for ( i = 1; i <= 8; i++ ) { // スレッドを追加・生成 for ( j = 0; j < Step; j++ ) { CFoo *p = new CFoo( &sema ); 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 < 100000; j++ ) { sema.V(); } printf( "Thread Count %d : time = %d\n", vFoo.size(), time( NULL ) - t1 ); } return 0; }