From 7b14aeb53e20727d304f97de33739c69770f2119 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 18 Nov 2020 22:42:00 -0500 Subject: [PATCH] Tune thread count based on hardware concurrency. --- osdep/LinuxEthernetTap.cpp | 12 ++++++------ osdep/LinuxEthernetTap.hpp | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/osdep/LinuxEthernetTap.cpp b/osdep/LinuxEthernetTap.cpp index 8a557c077..aeffede06 100644 --- a/osdep/LinuxEthernetTap.cpp +++ b/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 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::iterator t(_tapReaderThreads.begin());t!=_tapReaderThreads.end();++t) + t->join(); _tapProcessorThread.join(); for(std::vector::iterator i(_buffers.begin());i!=_buffers.end();++i) diff --git a/osdep/LinuxEthernetTap.hpp b/osdep/LinuxEthernetTap.hpp index a21a53a2c..8a3890bcb 100644 --- a/osdep/LinuxEthernetTap.hpp +++ b/osdep/LinuxEthernetTap.hpp @@ -70,7 +70,7 @@ private: int _fd; int _shutdownSignalPipe[2]; std::atomic_bool _enabled; - std::thread _tapReaderThread[2]; + std::vector _tapReaderThreads; std::thread _tapProcessorThread; std::mutex _buffers_l; std::vector _buffers;