Alex Bikfalvi
SimStream Documentation
SimWorkers.cpp
00001 #include "Headers.h" 00002 #include "SimWorkers.h" 00003 00004 CSimWorkers::CSimWorkers( 00005 unsigned int workersCount, 00006 unsigned int workersQueueSize 00007 ) 00008 { 00009 this->workersCount = workersCount; 00010 this->workerLast = 0; 00011 00012 // Create workers 00013 this->workers = new CSimWorker*[this->workersCount]; 00014 for(unsigned int index = 0; index < this->workersCount; index++) 00015 this->workers[index] = new CSimWorker(index, workersQueueSize); 00016 } 00017 00018 CSimWorkers::~CSimWorkers() 00019 { 00020 for(unsigned int index = 0; index < this->workersCount; index++) 00021 delete this->workers[index]; 00022 delete[] this->workers; 00023 } 00024 00025 void CSimWorkers::Start() 00026 { 00027 cout << "\nStarting simulator worker threads : "; 00028 // Start all workers 00029 for(unsigned int index = 0; index < this->workersCount; index++) 00030 { 00031 // Start worker 00032 this->workers[index]->Start(); 00033 cout << index << " "; 00034 } 00035 cout << "done!" << endl << endl; 00036 } 00037 00038 void CSimWorkers::Stop() 00039 { 00040 cout << "\nStopping simulator worker threads : "; 00041 // Stop all workers 00042 for(unsigned int index = 0; index < this->workersCount; index++) 00043 { 00044 // Stop worker 00045 this->workers[index]->Stop(); 00046 cout << index << " "; 00047 } 00048 cout << "done!" << endl << endl; 00049 } 00050 00051 void CSimWorkers::Enqueue(CSimWorkItem* item) 00052 { 00053 // Enqueue a work item on the current worker 00054 this->workers[this->workerLast]->Enqueue(item); 00055 00056 // Select next worker 00057 this->workerLast = (++this->workerLast) % this->workersCount; 00058 }
Last updated: February 8, 2011