Bug/30 server connection problem (#31)

* use threads instead of std::async, need to cleanup threads

* less buggy server connection per thread system
This commit is contained in:
Benjamin Sergeant 2019-04-16 22:19:44 -07:00 committed by GitHub
parent bcf2fc1812
commit 4e2e14fb22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -136,6 +136,12 @@ namespace ix
void SocketServer::stop()
{
for (auto&& thread : _connectionsThreads)
{
if (!thread.joinable()) continue;
thread.join();
}
if (!_thread.joinable()) return; // nothing to do
_stop = true;
@ -157,9 +163,6 @@ namespace ix
// Set the socket to non blocking mode, so that accept calls are not blocking
SocketConnect::configure(_serverFd);
// Return value of std::async, ignored
std::future<void> f;
for (;;)
{
if (_stop) return;
@ -228,14 +231,10 @@ namespace ix
}
// Launch the handleConnection work asynchronously in its own thread.
//
// the destructor of a future returned by std::async blocks,
// so we need to declare it outside of this loop
f = std::async(std::launch::async,
&SocketServer::handleConnection,
this,
clientFd,
connectionState);
_connectionsThreads.push_back(std::thread(&SocketServer::handleConnection,
this,
clientFd,
connectionState));
}
}
}

View File

@ -12,6 +12,7 @@
#include <string>
#include <set>
#include <thread>
#include <vector>
#include <mutex>
#include <functional>
#include <memory>
@ -63,6 +64,8 @@ namespace ix
std::atomic<bool> _stop;
std::thread _thread;
std::vector<std::thread> _connectionsThreads;
std::condition_variable _conditionVariable;
std::mutex _conditionVariableMutex;