ADD: added asyncworker to utilsfunctions
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user