1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

fixed possible race condition

This commit is contained in:
AlexVinS 2017-07-12 19:40:14 +03:00
parent 78885b8acc
commit 09df2dcfbb
3 changed files with 19 additions and 9 deletions

View File

@ -1355,8 +1355,10 @@ void startGame(StartInfo * options, CConnection *serv/* = nullptr*/)
client->loadGame(fname,vm.count("loadserver"),vm.count("loadhumanplayerindices") ? vm["loadhumanplayerindices"].as<std::vector<int>>() : std::vector<int>(),vm.count("loadnumplayers") ? vm["loadnumplayers"].as<int>() : 1,vm["loadplayer"].as<int>(),vm.count("loadserverip") ? vm["loadserverip"].as<std::string>() : "", vm.count("loadserverport") ? vm["loadserverport"].as<ui16>() : CServerHandler::getDefaultPort()); client->loadGame(fname,vm.count("loadserver"),vm.count("loadhumanplayerindices") ? vm["loadhumanplayerindices"].as<std::vector<int>>() : std::vector<int>(),vm.count("loadnumplayers") ? vm["loadnumplayers"].as<int>() : 1,vm["loadplayer"].as<int>(),vm.count("loadserverip") ? vm["loadserverip"].as<std::string>() : "", vm.count("loadserverport") ? vm["loadserverport"].as<ui16>() : CServerHandler::getDefaultPort());
break; break;
} }
{
client->connectionHandler = new boost::thread(&CClient::run, client); TLockGuard _(client->connectionHandlerMutex);
client->connectionHandler = make_unique<boost::thread>(&CClient::run, client);
}
} }
void endGame() void endGame()

View File

@ -113,7 +113,10 @@ void CClient::init()
{ {
waitingRequest.clear(); waitingRequest.clear();
hotSeat = false; hotSeat = false;
connectionHandler = nullptr; {
TLockGuard _(connectionHandlerMutex);
connectionHandler.reset();
}
pathInfo = nullptr; pathInfo = nullptr;
applier = new CApplier<CBaseForCLApply>; applier = new CApplier<CBaseForCLApply>;
registerTypesClientPacks1(*applier); registerTypesClientPacks1(*applier);
@ -710,15 +713,19 @@ void CClient::stopConnection()
} }
} }
if(connectionHandler)//end connection handler
{ {
if(connectionHandler->get_id() != boost::this_thread::get_id()) TLockGuard _(connectionHandlerMutex);
connectionHandler->join(); if(connectionHandler)//end connection handler
{
if(connectionHandler->get_id() != boost::this_thread::get_id())
connectionHandler->join();
logNetwork->infoStream() << "Connection handler thread joined"; logNetwork->infoStream() << "Connection handler thread joined";
vstd::clear_pointer(connectionHandler); connectionHandler.reset();
}
} }
if (serv) //and delete connection if (serv) //and delete connection
{ {
serv->close(); serv->close();

View File

@ -172,7 +172,8 @@ public:
const CPathsInfo * getPathsInfo(const CGHeroInstance *h); const CPathsInfo * getPathsInfo(const CGHeroInstance *h);
bool terminate; // tell to terminate bool terminate; // tell to terminate
boost::thread *connectionHandler; //thread running run() method std::unique_ptr<boost::thread> connectionHandler; //thread running run() method
boost::mutex connectionHandlerMutex;
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
virtual PlayerColor getLocalPlayer() const override; virtual PlayerColor getLocalPlayer() const override;