one therad is enough

This commit is contained in:
maliakal_d 2022-02-01 12:23:57 +01:00
parent ca8a1c046a
commit c236cbf17b
2 changed files with 21 additions and 42 deletions

View File

@ -18,37 +18,23 @@ void ThreadArping::StartRunning() {
if (arpInterfaceIp.size() == 0) { if (arpInterfaceIp.size() == 0) {
throw sls::RuntimeError("No Interface added to Arping"); throw sls::RuntimeError("No Interface added to Arping");
} }
threads.clear();
threadIds.clear();
runningFlag = true; runningFlag = true;
// create threadss // create thread
for (auto arp : arpInterfaceIp) {
try { try {
std::thread temp = std::thread temp = std::thread(&ThreadArping::RunningThread, this);
std::thread(&ThreadArping::RunningThread, this, threadObject = temp.native_handle();
threads.size(), arp.first, arp.second);
threads.push_back(temp.native_handle());
temp.detach(); temp.detach();
} catch (...) { } catch (...) {
StopRunning(); throw sls::RuntimeError("Could not create arping thread");
throw sls::RuntimeError("Could not create arping thread [" +
arp.first + ", " + arp.second + "]");
}
} }
} }
} }
void ThreadArping::StopRunning() { void ThreadArping::StopRunning() {
int i = 0; pthread_cancel(threadObject);
for (auto t : threads) { LOG(logINFOBLUE) << "Killing [ Arping Thread, Tid: " << threadId << "]";
pthread_cancel(t);
LOG(logINFOBLUE) << "Killing [ Arping Thread " << i << ": ("
<< arpInterfaceIp[i].first << ", "
<< arpInterfaceIp[i].second << ")]";
++i;
} }
threads.clear();
runningFlag = false; runningFlag = false;
} }
@ -58,22 +44,17 @@ void ThreadArping::AddIpsAndInterfaces(std::string interface, std::string ip) {
arpInterfaceIp.push_back(std::make_pair(interface, ip)); arpInterfaceIp.push_back(std::make_pair(interface, ip));
} }
void ThreadArping::RunningThread(int index, std::string interface, void ThreadArping::RunningThread() {
std::string ip) {
pid_t threadId = syscall(SYS_gettid); threadId = syscall(SYS_gettid);
LOG(logINFOBLUE) << "Created [ Arping Thread " << index << ": (" LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << "]";
<< interface << ", " << ip << ") Tid: " << threadId << "]";
{
std::lock_guard<std::mutex> lock(&mutexIds);
threadIds.push_back(threadId);
}
while (IsRunning()) { while (IsRunning()) {
LOG(logINFOBLUE) << "Going to sleep apring id " << threadId; LOG(logINFOBLUE) << "Going to sleep";
// wait for 60s // wait for 60s
usleep(60 * 1000 * 1000); usleep(60 * 1000 * 1000);
} }
LOG(logINFOBLUE) << "Exiting [ Arping Thread " << index << ": (" LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << "]";
<< interface << ", " << ip << ") Tid: " << threadId << "]";
} }

View File

@ -10,7 +10,6 @@ listening to.
#include "sls/sls_detector_defs.h" #include "sls/sls_detector_defs.h"
#include <atomic> #include <atomic>
#include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <utility> // pair, make_pair #include <utility> // pair, make_pair
@ -21,10 +20,9 @@ class ThreadArping : private virtual slsDetectorDefs {
std::atomic<bool> killThread{false}; std::atomic<bool> killThread{false};
std::atomic<bool> runningFlag{false}; std::atomic<bool> runningFlag{false};
std::vector<pthread_t> threads; pthread_t threadObject;
std::vector<std::pair<std::string, std::string>> arpInterfaceIp; std::vector<std::pair<std::string, std::string>> arpInterfaceIp;
std::vector<pid_t> threadIds; pid_t threadIds;
std::mutex mutexIds;
public: public:
ThreadArping(); ThreadArping();
@ -41,5 +39,5 @@ class ThreadArping : private virtual slsDetectorDefs {
* RunningMask is satisfied Then it exits the thread on its own if * RunningMask is satisfied Then it exits the thread on its own if
* killThread is true * killThread is true
*/ */
void RunningThread(int index, std::string interface, std::string ip); void RunningThread(std::string interface, std::string ip);
}; };