less buggy server connection per thread system
This commit is contained in:
		| @@ -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,11 +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; | ||||
|  | ||||
|         std::vector<std::thread> threads; | ||||
|  | ||||
|         for (;;) | ||||
|         { | ||||
|             if (_stop) return; | ||||
| @@ -230,15 +231,7 @@ 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); | ||||
|             threads.push_back(std::thread(&SocketServer::handleConnection, | ||||
|             _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; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user