coins/ExecuteCoins.cpp
/**
 * @brief: Executes the algorithm using a vector to store new threads which are larger 
 *           joined to make sure no thread is left open and all inputs are accounted for.
 *         Uses a semaphore for thread count regulation. sem_wait slowly decremented until
 *         it reaches 0, at which point, no new threads will be deployed.
 */
@brief: Executes the algorithm using a vector to store new threads which are larger
joined to make sure no thread is left open and all inputs are accounted for.
Uses a semaphore for thread count regulation. sem_wait slowly decremented until
it reaches 0, at which point, no new threads will be deployed.
@brief : Executes the algorithm using a vector to store new threads which are larger joined to make sure no thread is left open and all inputs are accounted for . Uses a semaphore for thread count regulation . sem_wait slowly decremented until it reaches 0 at which point no new threads will be deployed .
results ExecuteCoins::initializeMulti(params p)
{
    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);
    auto startTotal = std::chrono::high_resolution_clock::now();
    int count = 0;
    while (count < size) {
        std::vector<std::thread> threads(std::min(10000,(size-count)));
        for (size_t i = 0; i < threads.size(); ++i) {
            sem_wait(&sem); 
            std::vector<int> arr = inpVect.at(count+i); 
            coin ans = ansVect.at(count+i);
            threads[i] = std::thread(execMulti,arr,ans);
        }
        for (size_t i = 0; i < threads.size(); ++i) {
            threads[i].join();
            count++;
        }
    }
    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 :: initializeMulti ( params p ) { 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 ) ; auto startTotal = std :: chrono :: high_resolution_clock :: now ( ) ; int count = 0 ; while ( count < size ) { std :: vector < std :: thread > threads ( std :: min ( 10000 , ( size - count ) ) ) ; for ( size_t i = 0 ; i < threads . size ( ) ; ++ i ) { sem_wait ( & sem ) ; std :: vector < int > arr = inpVect . at ( count + i ) ; coin ans = ansVect . at ( count + i ) ; threads [ i ] = std :: thread ( execMulti , arr , ans ) ; } for ( size_t i = 0 ; i < threads . size ( ) ; ++ i ) { threads [ i ] . join ( ) ; count ++ ; } } 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 vector to store new threads which are larger
           joined to make sure no thread is left open and all inputs are accounted for.
@brief : Executes the algorithm using a vector to store new threads which are larger joined to make sure no thread is left open and all inputs are accounted for .
// Execute the algo /* Using a normal system of pushing_back every thread created a problem
    *  when the thread count exceed 32,737. As a result, this method was adapted
    *  where the thread vector has a max, once it hit, it will join all threads
    *  and then repeat. Avoiding the vector becoming too large. */ // Limits the active number of threads to equal p.threads // .at() is O(1) time complexity