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