// // compile with // // g++ -std=c++0x -pthread -O2 -o TaskThreadPrime_TMan TaskThreadPrime_TMan.cpp // #include #include #include #include #include #include using namespace std; class ITaskManager { // Taskmanager interface, Singleton protected: static ITaskManager * instance; ITaskManager() {}; // private Konstruktor public: static ITaskManager * getInstance() { if(instance==0) { // Lazy instantiation std::cerr << " Error calling interface getInstance() before initializing derived class " << std::endl; std::exit(1); } return instance; }; virtual bool executeTask(int id=0) =0; }; ITaskManager * ITaskManager::instance = 0; class PrimeTask : public ITaskManager { // Klasse fuer Task als Singleton private: PrimeTask(); // private Konstruktor void InitPrimes(); int findPrimes( long start, long end, vector & pvec); vector m_primeRef; vector< vector< long > > m_primeOut; int m_task; int m_nt; mutex m_mutex; static const long IFIRST = 3; static const long INTERV_LEN = 1000000; static const long PRIME_MAX = 100000000; //static const long PRIME_MAX = 20000000; public: int getNPrimes() { int count = 0; for ( unsigned int i=0; i (instance) ; }; virtual bool executeTask( int id=0); }; PrimeTask::PrimeTask() // Konstruktor { InitPrimes(); m_nt = PRIME_MAX/INTERV_LEN; m_primeOut.resize( m_nt ); } void PrimeTask::InitPrimes() { m_primeRef.push_back(2); m_primeRef.push_back(3); for ( long p = 5; p*p < 2*PRIME_MAX; p+=2 ) { int index = 0; long t = m_primeRef[index]; // take only primes to test while (t*t <= p) { // and only those smaller sqrt(p) if ( p % t == 0 ) { // not prime, give up break; } index ++; t = m_primeRef[index]; } if (t*t > p ) { // found prime m_primeRef.push_back(p); // append to list } } } int PrimeTask::findPrimes( long start, long end, vector & pvec) { // check 1st we have enough ref-primes int ind = m_primeRef.size() - 1; if ( m_primeRef[ind] * m_primeRef[ind] < end ) { cout << "Error, not enough ref-primes " << m_primeRef[ind] << " " << end << endl; return -1; } int npr = 0; for ( long p = start; p < end; p+=2 ) { int index = 0; long t = m_primeRef[index]; // take only primes to test while (t*t <= p) { // and only those smaller sqrt(p) if ( p % t == 0 ) { // not prime, give up break; } index ++; t = m_primeRef[index]; } if (t*t > p ) { // found prime pvec.push_back(p); // append to list npr ++; } } return(npr); } bool PrimeTask::executeTask( int id) { // std::cout << " called PrimeTask::executeTask() " << std::endl; // ensure exclusive access on member var m_task m_mutex.lock(); int itsk = m_task ++; m_mutex.unlock(); if ( itsk >= m_nt ) return( false ); long start = INTERV_LEN * itsk + IFIRST; long end = start + INTERV_LEN; int npr = findPrimes( start, end, m_primeOut[itsk]); std::cout << "Thread " << id << " executed task " << itsk << " found " << npr << " primes " << std::endl; return(true); } int runTask( int id ) { ITaskManager * itp = ITaskManager::getInstance(); while ( itp->executeTask(id) ) ; return 0; } int main() { PrimeTask * tp = PrimeTask::getInstance(); int n_thread = 10; vector> vf; for ( unsigned int i=0; igetNPrimes() << endl; // while ( tp->executeTask()); return 0; }