vendredi 31 juillet 2015

Using existing std::thread(s) for doing work

I have an algorithm that takes a long time to run (hours to days) with many iterations of the same loop.

I want to use the std::thread library as part of C++ 11 as much as possible.

I would like to start up nTread worker threads and then have them sit idle until instructed to do operation "taskNumber".

Can I do this with future and promises?

Is there a better way to do this?

In the below code what I am trying to do is:

  1. Start up 6 worker threads. They start off waiting to do any work.
  2. Tell the 6 worker threads to do task 0. The main thread waits for them all to completed.
  3. Tell the 6 worker threads to do task 1. The main thread waits for them all to completed.
  4. Tell the 6 worker threads to do task 2. The main thread waits for them all to completed.
  5. Tell the 6 worker threads to do task 3. The main thread waits for them all to completed.
  6. repeat steps 1) - 1) 100 times.

What I am trying is as follows:

#include <iostream>
#include <thread>
#include <future>
#include <vector>

std::vector<std::promise<int>> readyPromise;
std::vector<std::promise<bool>> donePromise;

void initiazer(int threadNumber)
{
    while (true) {
        // Wait to see if it is ready to run
        std::future<int> readyFuture = readyPromise[threadNumber].get_future();
        int taskNumber = readyFuture.get();

        std::cout<<"Inside Thread: " << threadNumber << " doing task: " << taskNumber << std::endl;

        // tell it that this is done
        donePromise[threadNumber].set_value(true);

        if (taskNumber == 9)
            exit(0);
    }
}

int main()
{
    int nThread = 6;

    // Create the promises and future
    std::vector<std::future<bool>> doneFuture;
    for (int i=0; i<=nThread; ++i) {
        readyPromise.push_back(std::promise<int>());
        donePromise.push_back(std::promise<bool>());
        doneFuture.push_back(std::future<bool>());
    }

    // Create the threads
    std::vector<std::thread> threads;
    std::cout << "Create nThread threads" << std::endl;
    for (int i=0; i<=nThread; ++i)
        threads.push_back(std::thread(initiazer, i));

    // Loop 100 times
    for (int j=0; j<100; j++) {
        for (int task=0; task<4; j++) {
            // tell the threads to go
            for (int i=0; i<nThread; i++) {
                doneFuture[i] = donePromise[nThread].get_future();
                readyPromise[i].set_value(task);
            }

            // Nothing happening here.

            // Wait for the threads to finish
            for (int i=0; i<nThread; i++) {
                doneFuture[i].get();
            }
            // Do some work on the main thread.
        }
    }

    int task = 9; // Tell threads to exit
    for (int i=0; i<nThread; i++) {
        doneFuture[i] = donePromise[nThread].get_future();
        readyPromise[i].set_value(task);
    }

    // Wait for all the threads to exit.
    for (int i=0; i<nThread; i++) {
        threads[i].join();
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire