How to stop std thread safely?

Viewed 20559

I'm developing a chat server and I have a question.

How to stop std::thread safely?

it is very easy problem, like this.

thread t(&func);
t.join();

but, if func is has infinite loop, join is not working.

this is my source.

void CServerSocket::AcceptRun(boost::asio::io_service &iosrv)
{
    while (true)
    {
        auto sock = std::make_shared<boost::asio::ip::tcp::socket>(iosrv);
        m_Acceptor->accept(*sock);

        m_SocketList.push_back(std::make_shared<CConnectionSocket>(this, sock));
    }
}

and

CServerSocket::~CServerSocket()
{
    CLogManager::WriteLog("Stopping Server...");
    m_Acceptor->close();
    m_Acceptor.reset();

    // m_AcceptThread.detach(); This is right?

    CLogManager::WriteLog("Server Stoped!");
}

I'm very wondering. Please help me. thank you.

3 Answers
Related