调用join时Boost :: Thread访问冲突

时间:2011-08-06 18:00:45

标签: c++ multithreading boost join

我在尝试创建多线程服务器时遇到了一些问题。一切正常,直到我需要从服务器中删除客户端。

服务器在它自己的线程中运行,然后每个客户端也有它自己的线程。

我正在为所有线程使用boost :: thread。当我需要停止客户端时,我打电话

void StopClient()
{
    assert(mThread);

    mStopMutex.lock();
    mStopRequested = true;
    mStopMutex.unlock();

    shutdown(mSocket,2);

    mThread->join();
}

在行中添加断点

shutdown(mSocket,2);

我可以看到mThread不存在!这是否意味着线程已经退出?你总是需要为boost :: thread调用join()吗?

如果我允许代码运行,则会出现访问冲突错误。

更新

ServerThread

void StartServer() 
{
    assert(!mListenThread);
    mListenThread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&ServerThread::Listen, this)));
    mUpdateThread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&ServerThread::Update, this)));
}

void StopServer()
{
    assert(mListenThread);
    mStopRequested = true;

            mMutex.lock();
            for(int i = 0; i < mClients.size(); i++ )
                mClients[i]->StopClient();
            mMutex.unlock();

    mListenThread->join();
}

void Listen()
{
    while (!mStopRequested)
    {
        std::cout << "Waiting for connection" << std::endl;         
        if(mClientSocket = accept( mServerSocket, (sockaddr*) &mServerAddr, &addrlen ) )
        {
            mMutex.lock();
            if( mClients.size() > 0 )
            {
                for( int i = 0; i < mClients.size(); i++ )
                {
                    if( mClients[i]->getClientSocket() != mClientSocket )
                    {
                        ClientThread newClient;
                        newClient.Initialise(mClientSocket);
                        mClients.push_back(&newClient);
                        mClients[mClients.size()-1]->StartClient();
                        break;
                    }
                }
            }
            else
            {
                ClientThread newClient;
                newClient.Initialise(mClientSocket);
                mClients.push_back(&newClient);
                mClients[mClients.size()-1]->StartClient();
            }
            mMutex.unlock();
        }
    }
}          

void Update()
{
    while (!mStopRequested)
    {
        mMutex.lock();

        std::cout << "::::Server is updating!::::" << mClients.size() << std::endl;
        for( int i = 0; i< mClients.size(); i++ )
        {
            if( !mClients[i]->IsActive() )
            {
                mClients[i]->StopClient();
                mClients.erase( mClients.begin() + i );
            }
        }

        mMutex.unlock();

    }
}

ClientThread

void StartClient()
{
    assert(!mThread);
    mThread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&ClientThread::Update, this)));
}

void Update()
{
    bool stopRequested;
    do
    {
        mStopMutex.lock();
        stopRequested = mStopRequested;
        mStopMutex.unlock();

        std::cout << "lol" << std::endl;
        if( mTimeOut < 1000 )
        {
            mTimeOut++;
        }
        else
        {
            mActive = false;
        }

        boost::this_thread::interruption_point();
    }
    while( !stopRequested);
}

2 个答案:

答案 0 :(得分:1)

            ClientThread newClient;
            newClient.Initialise(mClientSocket);
            mClients.push_back(&newClient);

这会在堆栈上创建一个局部变量,并将其地址放入mClients列表中。然后范围结束,局部变量也是如此。这使您的列表mClients指向不再存在的内容。

答案 1 :(得分:0)

这里没有足够的代码来确定究竟发生了什么,但这里有待检查的事项:

  • 您的客户端线程中可能有一个正在检查mStopRequested成员的处理循环。在处理循环退出之前,它是否正在更改mThread成员?
  • 获得访问冲突时mThread是否为空?或者,它还有其他价值吗?在将mStopRequested设置为true之前有效吗?