Tiny threads fixes & a little bit of safety

This commit is contained in:
k1-801 2020-11-15 16:00:59 +04:00 committed by Adam Honse
parent 9a53709db0
commit dde857dfb4
18 changed files with 85 additions and 154 deletions

View file

@ -261,12 +261,21 @@ RGBController_E131::RGBController_E131(std::vector<E131Device> device_list)
if(keepalive_delay.count() > 0)
{
KeepaliveThread = new std::thread(&RGBController_E131::KeepaliveThreadFunction, this);
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&RGBController_E131::KeepaliveThreadFunction, this);
}
else
{
keepalive_thread_run = 0;
keepalive_thread = nullptr;
}
}
RGBController_E131::~RGBController_E131()
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
/*---------------------------------------------------------*\
| Delete the matrix map |
\*---------------------------------------------------------*/
@ -413,7 +422,7 @@ void RGBController_E131::DeviceUpdateMode()
void RGBController_E131::KeepaliveThreadFunction()
{
while(1)
while(keepalive_thread_run.load())
{
if((std::chrono::steady_clock::now() - last_update_time) > ( keepalive_delay * 0.95f ) )
{

View file

@ -78,7 +78,8 @@ private:
std::vector<e131_addr_t> dest_addrs;
std::vector<unsigned int> universes;
int sockfd;
std::thread * KeepaliveThread;
std::thread * keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::milliseconds keepalive_delay;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};