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

View File

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