progschj / ThreadPool

A simple C++11 Thread Pool implementation
zlib License
7.64k stars 2.21k forks source link

main thread block on notify_one():sorry I didn't notice the former issue. #43

Closed ghost closed 7 years ago

ghost commented 7 years ago

When you change the example.cpp as follows(1.more than 1000 loop times and 2.delete cout in work function):

int main()
{
    m::MThreadPool pool(4);
    std::vector< std::future<int> > results;
    for(int i = 0; i < 10000; ++i) {
        results.emplace_back(
            pool.enqueue([i] {
                return i*i;
            })
        );
    }
    for(auto && result: results)
        result.get();

    return 0;
}

The program is easy to block. I add some output in enqueue function and thread. This is the output when the program block. all We can see, when notify_one() was call, two threads were blocking on condition.wait() and one thread was in calculating and one thread was waiting for mtx.lock().

I can't understand why the main thread will block.