2
0
Эх сурвалжийг харах

Tune thread count based on hardware concurrency.

Adam Ierymenko 4 жил өмнө
parent
commit
7b14aeb53e

+ 6 - 6
osdep/LinuxEthernetTap.cpp

@@ -184,14 +184,14 @@ LinuxEthernetTap::LinuxEthernetTap(
 
 	(void)::pipe(_shutdownSignalPipe);
 
-	for(unsigned int t=0;t<2;++t) {
-		_tapReaderThread[t] = std::thread([this, t]{
+	for(unsigned int tn=0,tc=(unsigned int)std::thread::hardware_concurrency();tn<=tc;++tn) {
+		_tapReaderThreads.push_back(std::thread([this, tn]{
 			fd_set readfds,nullfds;
 			int n,nfds,r;
 			void *buf = nullptr;
 			std::vector<void *> buffers;
 
-			if (t == 0) {
+			if (tn == 0) {
 				struct ifreq ifr;
 				memset(&ifr,0,sizeof(ifr));
 				strcpy(ifr.ifr_name,_dev.c_str());
@@ -298,7 +298,7 @@ LinuxEthernetTap::LinuxEthernetTap(
 					}
 				}
 			}
-		});
+		}));
 	}
 
 	_tapProcessorThread = std::thread([this] {
@@ -331,8 +331,8 @@ LinuxEthernetTap::~LinuxEthernetTap()
 	::close(_shutdownSignalPipe[0]);
 	::close(_shutdownSignalPipe[1]);
 
-	_tapReaderThread[0].join();
-	_tapReaderThread[1].join();
+	for(std::vector<std::thread>::iterator t(_tapReaderThreads.begin());t!=_tapReaderThreads.end();++t)
+		t->join();
 	_tapProcessorThread.join();
 
 	for(std::vector<void *>::iterator i(_buffers.begin());i!=_buffers.end();++i)

+ 1 - 1
osdep/LinuxEthernetTap.hpp

@@ -70,7 +70,7 @@ private:
 	int _fd;
 	int _shutdownSignalPipe[2];
 	std::atomic_bool _enabled;
-	std::thread _tapReaderThread[2];
+	std::vector<std::thread> _tapReaderThreads;
 	std::thread _tapProcessorThread;
 	std::mutex _buffers_l;
 	std::vector<void *> _buffers;