vendredi 2 juin 2017

this_thread::sleep_for affecting other thread

I have a simple program with two threads, one that pushes a packaged_task into a deque, and other that executes it. In the tasks there is a this_thread::sleep_for, and I would expect that only the "process" thread would wait for it, but both are waiting, making the execution sequential. What I'm missing?

#include <future>
#include <iostream>
#include <deque>

std::mutex m;
std::condition_variable cv;
std::deque<std::packaged_task<void(int)>> deque;

void post() {
    int id = 0;
    auto lambda = [](int id) {
        std::this_thread::sleep_for(std::chrono::seconds(std::rand() % 10 + 1)); 
        std::cout << id << std::endl;
    };
    while (true) {
        std::this_thread::sleep_for(std::chrono::seconds(1));

        std::packaged_task<void(int)> task(lambda);
        task(id++);

        std::lock_guard<std::mutex> lg(m);
        deque.emplace_back(std::move(task));

        cv.notify_one();
    }
}

void process() {
    std::deque<std::packaged_task<void(int)>> to_exec;
    while (true) {

        while (!to_exec.empty()){
            std::future<void> fut = to_exec.front().get_future();
            fut.get();

            to_exec.pop_front();
        }

        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, []() {return !deque.empty(); });

        while (!deque.empty()) {
            to_exec.push_back(std::move(deque.front()));
            deque.pop_front();
        }
    }
}

int main() {

    std::thread tpost(post);
    std::thread tprocess(process);

    tpost.join();
    tprocess.join();
}

Aucun commentaire:

Enregistrer un commentaire