Browse Source

Set thread pool size according to hardware concurrency

Paul-Louis Ageneau 2 years ago
parent
commit
753b66caa4
2 changed files with 9 additions and 3 deletions
  1. 8 2
      src/impl/init.cpp
  2. 1 1
      src/impl/internals.hpp

+ 8 - 2
src/impl/init.cpp

@@ -11,9 +11,9 @@
 
 #include "certificate.hpp"
 #include "dtlstransport.hpp"
+#include "icetransport.hpp"
 #include "pollservice.hpp"
 #include "sctptransport.hpp"
-#include "icetransport.hpp"
 #include "threadpool.hpp"
 #include "tls.hpp"
 
@@ -29,6 +29,8 @@
 #include <winsock2.h>
 #endif
 
+#include <thread>
+
 namespace rtc::impl {
 
 struct Init::TokenPayload {
@@ -115,7 +117,11 @@ void Init::doInit() {
 		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
 #endif
 
-	ThreadPool::Instance().spawn(THREADPOOL_SIZE);
+	int concurrency = std::thread::hardware_concurrency();
+	int count = std::max(concurrency, MIN_THREADPOOL_SIZE);
+	PLOG_DEBUG << "Spawning " << count << " threads";
+	ThreadPool::Instance().spawn(count);
+
 #if RTC_ENABLE_WEBSOCKET
 	PollService::Instance().start();
 #endif

+ 1 - 1
src/impl/internals.hpp

@@ -43,7 +43,7 @@ const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536; // Remote max message size if not
 
 const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size
 
-const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool (>= 2)
+const int MIN_THREADPOOL_SIZE = 4; // Minimum number of threads in the global thread pool (>= 2)
 
 const size_t DEFAULT_MTU = RTC_DEFAULT_MTU; // defined in rtc.h