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()
|
void SocketServer::stop()
|
||||||
{
|
{
|
||||||
|
for (auto&& thread : _connectionsThreads)
|
||||||
|
{
|
||||||
|
if (!thread.joinable()) continue;
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
if (!_thread.joinable()) return; // nothing to do
|
if (!_thread.joinable()) return; // nothing to do
|
||||||
|
|
||||||
_stop = true;
|
_stop = true;
|
||||||
@ -157,9 +163,6 @@ namespace ix
|
|||||||
// Set the socket to non blocking mode, so that accept calls are not blocking
|
// Set the socket to non blocking mode, so that accept calls are not blocking
|
||||||
SocketConnect::configure(_serverFd);
|
SocketConnect::configure(_serverFd);
|
||||||
|
|
||||||
// Return value of std::async, ignored
|
|
||||||
std::future<void> f;
|
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
if (_stop) return;
|
if (_stop) return;
|
||||||
@ -228,14 +231,10 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Launch the handleConnection work asynchronously in its own thread.
|
// Launch the handleConnection work asynchronously in its own thread.
|
||||||
//
|
_connectionsThreads.push_back(std::thread(&SocketServer::handleConnection,
|
||||||
// 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,
|
this,
|
||||||
clientFd,
|
clientFd,
|
||||||
connectionState);
|
connectionState));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -63,6 +64,8 @@ namespace ix
|
|||||||
std::atomic<bool> _stop;
|
std::atomic<bool> _stop;
|
||||||
std::thread _thread;
|
std::thread _thread;
|
||||||
|
|
||||||
|
std::vector<std::thread> _connectionsThreads;
|
||||||
|
|
||||||
std::condition_variable _conditionVariable;
|
std::condition_variable _conditionVariable;
|
||||||
std::mutex _conditionVariableMutex;
|
std::mutex _conditionVariableMutex;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user