ADD: added asyncworker to utilsfunctions

This commit is contained in:
hwinkel
2023-06-07 22:46:40 +02:00
parent f83fabd343
commit d4c12f3b09

View File

@@ -4,6 +4,9 @@
#include <sstream> #include <sstream>
#include <netdb.h> #include <netdb.h>
#include <iostream>
#include <thread>
#include <atomic>
namespace SimCore { namespace SimCore {
@@ -25,4 +28,33 @@ namespace SimCore {
}; };
class AsyncWorker {
std::atomic<bool> active{true};
public:
~AsyncWorker(){
stop();
}
void work(auto function)
{
active = true;
std::thread t([=,this]() {
while(active.load()) {
function();
}
});
t.detach();
}
void stop()
{
active = false;
}
};
} }