c++ - Boost::调用连接时线程访问冲突

标签 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()?

如果我允许代码运行,我会收到访问冲突错误。

更新

服务器线程

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();

    }
}

客户端线程

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);
}

最佳答案

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

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

关于c++ - Boost::调用连接时线程访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6968623/

相关文章:

创建输出存储而不是应用于现有存储的 C++ 算法?

c++ - 遍历文本文件并为每一行分配变量

c++ - 在这种情况下,有比dynamic_cast更好的解决方案吗?

c++ - 包含它的模板化类型的 shared_ptr

c++ - Boost gil 从 rgb8_view_t 获取缓冲区

c++ - 继承一个 std::container 包括它的 boost 序列化

c++ - 如何使用初始化列表来初始化 2D vector 成员?

c++ - 独立的 Windows 应用程序在焦点改变时挂起

c# - 使用 Winforms 和 C# 继续更新文本框

multithreading - Future::spawn() 有点没用?我怎样才能多线程呢?