修复线程无法正常结束
This commit is contained in:
parent
8bc2a2e0fe
commit
c7d47405ed
@ -50,11 +50,11 @@ void SerialPort::stop()
|
|||||||
stop_flag_ = true;
|
stop_flag_ = true;
|
||||||
running_ = false;
|
running_ = false;
|
||||||
|
|
||||||
if (reader_thread_.joinable()) reader_thread_.join();
|
// 先关闭 fd,打断 reader 的阻塞 read()
|
||||||
|
|
||||||
if (reconnect_thread_.joinable()) reconnect_thread_.join();
|
|
||||||
|
|
||||||
close_port();
|
close_port();
|
||||||
|
|
||||||
|
if (reader_thread_.joinable()) reader_thread_.join();
|
||||||
|
if (reconnect_thread_.joinable()) reconnect_thread_.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SerialPort::open_port()
|
bool SerialPort::open_port()
|
||||||
@ -138,6 +138,13 @@ void SerialPort::reader_loop()
|
|||||||
running_ = false;
|
running_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void interruptible_sleep(std::atomic<bool>& stop_flag, int seconds)
|
||||||
|
{
|
||||||
|
using namespace std::chrono;
|
||||||
|
for (int i = 0; i < seconds * 10 && !stop_flag.load(std::memory_order_relaxed); ++i)
|
||||||
|
std::this_thread::sleep_for(100ms);
|
||||||
|
}
|
||||||
|
|
||||||
void SerialPort::reconnect_loop()
|
void SerialPort::reconnect_loop()
|
||||||
{
|
{
|
||||||
int current_interval = retry_interval_;
|
int current_interval = retry_interval_;
|
||||||
@ -159,7 +166,7 @@ void SerialPort::reconnect_loop()
|
|||||||
{
|
{
|
||||||
LOG_INFO("[" + id_ + "] Connect failed, retry in " + std::to_string(current_interval) + "s");
|
LOG_INFO("[" + id_ + "] Connect failed, retry in " + std::to_string(current_interval) + "s");
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(current_interval));
|
interruptible_sleep(stop_flag_, current_interval);
|
||||||
current_interval = std::min(current_interval * 2, max_interval);
|
current_interval = std::min(current_interval * 2, max_interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user