'boost::threadpool::pool::wait() doesn't stop

I was trying to write some Task-Management class with C++ boost::threadpool, condition_variable and mutex. It seems the program will stop at boost::threadpool::pool::wait(), but I don't know why this happens.

#include <boost/threadpool.hpp>
#include <condition_variable>
#include <iostream>
#include <mutex>
using namespace std;

enum {
    Running,
    Stopped,
    Exiting
};

class C {
private:
    int                     m_iStatus; 
    mutex                   m_mtx;
    condition_variable      m_cond;
    boost::threadpool::pool m_tp;
public:
    C() : m_iStatus(Stopped), m_tp(8) {}
    void Start();
    void Exit();
private:
    bool Check();
    void Dispatcher();
};

bool C::Check()
{
    unique_lock<mutex> lk(m_mtx);
    if (m_iStatus == Stopped)
        m_cond.wait(lk);
    if (m_iStatus == Exiting)
        return false;
    else                
        return true;
}

void C::Dispatcher()
{
    if (!Check())
        return;

    unique_lock<mutex> lk(m_mtx);
    // do something...
    cout << "." << endl;

    m_tp.schedule(bind(&C::Dispatcher, this));
}

void C::Start()
{
    unique_lock<mutex> lk(m_mtx);
    m_iStatus = Running;
    m_tp.schedule(bind(&C::Dispatcher, this));
}

void C::Exit()
{
    unique_lock<mutex> lk(m_mtx);
    m_iStatus = Exiting;
    m_cond.notify_all(); /* notify those waiting on m_cond */
    m_tp.wait(); /* went wrong here */
}

int main()
{
    C c;
    c.Start();

    /* wait for a moment */
    Sleep(1000);

    /* then call Exit */
    c.Exit();
    
    return 0;
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source