mardi 12 juillet 2016

Making a queue of threads in C++

I'm trying to run threads one after the other. I know I could do them serially but the whole point is so the commands don't block the UI.

However, in the implementation I came up with, I can't use std::thread because I can't pass threads as a parameter.

Is there another way I can do this?


This is my current implementation:

#include <mutex>
#include <thread>
#include <queue>

class ThreadQueue {
    std::queue <std::thread> threads;
    std::mutex mutex_add;
    std::mutex mutex_remove;

public:
   ThreadQueue() {}

    ~ThreadQueue() {}

    void add(std::thread thread) {
        mutex_add.lock();
        threads.push(thread);
        mutex_add.unlock();
    }

    std::thread remove() {
        mutex_remove.lock();
        std::thread thread = threads.front;
        threads.pop();
        thread.join();
        mutex_remove.unlock();
    }
};

Aucun commentaire:

Enregistrer un commentaire