coins/ExecuteCoins.cpp
/**
 * @brief: Executes the algorithm using a threadpool in contrast to initalizeMulti, this will 
 *           reuse threads instead of creating and deleting a thread when necessary.
 *         ThreadPool is implemented from https://github.com/mtrebi/thread-pool.
 *         Uses a semaphore for thread count regulation. Used more in initalizeMulti.
 */
@brief: Executes the algorithm using a threadpool in contrast to initalizeMulti, this will
reuse threads instead of creating and deleting a thread when necessary.
@brief : Executes the algorithm using a threadpool in contrast to initalizeMulti this will reuse threads instead of creating and deleting a thread when necessary .
results ExecuteCoins::initializePool(params p)
{
    ThreadPool pool(p.threads); 
    pool.init(); 
    sem_init(&sem,0,p.threads);
    if (p.generateNew) twelvecoins.generate(p.count);
    std::vector<std::vector<int>> inpVect = twelvecoins.initInput();
    std::vector<coin> ansVect = twelvecoins.initAnswers();
    int size = std::min((int)ansVect.size(),p.count);
    int i = 0;
    auto startTotal = std::chrono::high_resolution_clock::now();
    for (std::vector<std::vector<int>>::iterator it = inpVect.begin() ; it != inpVect.end() && i < size; ++it, ++i) {
        sem_wait(&sem);
        std::vector<int> arr = *it;
        coin ans = ansVect.at(i);
        pool.submit(execMulti, arr, ans);           
    }
    usleep(10); 
    pool.shutdown();
    auto stopTotal = std::chrono::high_resolution_clock::now();
    durationTotal = std::chrono::duration_cast<std::chrono::microseconds>(stopTotal - startTotal);
    sem_destroy(&sem);
    r.correct = numCorrect;
    r.algoDuration = durationExec;
    r.functDuration = durationTotal;
    return r;
}
results ExecuteCoins :: initializePool ( params p ) { ThreadPool pool ( p . threads ) ; pool . init ( ) ; sem_init ( & sem , 0 , p . threads ) ; if ( p . generateNew ) twelvecoins . generate ( p . count ) ; std :: vector < std :: vector < int > > inpVect = twelvecoins . initInput ( ) ; std :: vector < coin > ansVect = twelvecoins . initAnswers ( ) ; int size = std :: min ( ( int ) ansVect . size ( ) , p . count ) ; int i = 0 ; auto startTotal = std :: chrono :: high_resolution_clock :: now ( ) ; for ( std :: vector < std :: vector < int > > :: iterator it = inpVect . begin ( ) ; it != inpVect . end ( ) && i < size ; ++ it , ++ i ) { sem_wait ( & sem ) ; std :: vector < int > arr = * it ; coin ans = ansVect . at ( i ) ; pool . submit ( execMulti , arr , ans ) ; } usleep ( 10 ) ; pool . shutdown ( ) ; auto stopTotal = std :: chrono :: high_resolution_clock :: now ( ) ; durationTotal = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( stopTotal - startTotal ) ; sem_destroy ( & sem ) ; r . correct = numCorrect ; r . algoDuration = durationExec ; r . functDuration = durationTotal ; return r ; }
@brief: Executes the algorithm using a threadpool in contrast to initalizeMulti, this will
           reuse threads instead of creating and deleting a thread when necessary.
@brief : Executes the algorithm using a threadpool in contrast to initalizeMulti this will reuse threads instead of creating and deleting a thread when necessary .
// Create pool with specified thread count // Initialize pool // Execute the algo // Prevents pool.shutdown() from hanging