jeudi 17 octobre 2019

spinning threadpool: floating point exception (core dumped)

I am implementing a spinning threadpool where threads continuously check for work in an infinite-loop. However, my implementation has the floating point exception.

I tried to print the output of each thread. The point where the exception occurs varies. It could be immediately after starting a bulk task-launch, while one of the threads are working on a task, or after all tasks in a bulk are completed.

TaskSystemParallelThreadPoolSpinning::TaskSystemParallelThreadPoolSpinning(int num_threads): ITaskSystem(num_threads) {
    //
    // TODO: CS149 student implementations may decide to perform setup
    // operations (such as thread pool construction) here.
    // Implementations are free to add new class member variables
    // (requiring changes to tasksys.h).
    //

    this->num_T = num_threads - 1;
    this->threads = new std::thread[num_threads - 1];
    this->mutex_ = new std::mutex();
    this->work_counter = 0;
    this->total_work = 0;

    for (int i = 0; i < this->num_T; i++) {
        this->threads[i] = std::thread(&TaskSystemParallelThreadPoolSpinning::waitFunc, this);
    }

}

static std::atomic<bool> done_flag = {false};

TaskSystemParallelThreadPoolSpinning::~TaskSystemParallelThreadPoolSpinning() {
    done_flag = true;

    for (int i = 0; i < this->num_T; i++) {
        this->threads[i].join();
    }

    delete this->mutex_;
    delete[] this->threads;
}

void TaskSystemParallelThreadPoolSpinning::waitFunc() {
//    std::unique_lock<std::mutex> lock(*this->workqueue.mutex_);

    while(done_flag==false){
//      this->workqueue.cond_var->wait(lock);  // wait on lock
        this->mutex_->lock();

        if (this->total_work==0) {  // NO work
            this->mutex_->unlock();
            continue;
        }

        int id = this->work_counter;
        if (id >= this->total_work) {  // ALL work done
            this->total_work = 0;
        std::cout << "All Work Done" << std::endl;
            this->mutex_->unlock();
            continue;
        }
    std::cout << id << std::endl;

        this->work_counter++;  // increment counter
        this->mutex_->unlock();
        this->runnable->runTask(id, this->total_work); // do work
    }
}

void TaskSystemParallelThreadPoolSpinning::run(IRunnable* runnable, int num_total_tasks) {


    //
    // TODO: CS149 students will modify the implementation of this
    // method in Part A.  The implementation provided below runs all
    // tasks sequentially on the calling thread.
    //
    this->mutex_->lock();
    this->runnable = runnable;
    this->work_counter = 0;
    this->total_work = num_total_tasks;
    std::cout << "Start Run" << std::endl;
    this->mutex_->unlock();

    bool done = false;
    while (done==false){
        this->mutex_->lock();
        if (this->work_counter > 0 && this->total_work == 0) {
            done = true;
        }
        this->mutex_->unlock();
    }

    std::cout << "Exit Run" << std::endl;
}

The output looks something like below.

Start Run
0
...
63
All Work Done
Exit Run
Start Run
0
...
61
62
63
All Work Done
Floating point exception (core dumped)

Aucun commentaire:

Enregistrer un commentaire