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:
parent
bcf2fc1812
commit
4e2e14fb22
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user