first commit
This commit is contained in:
parent
ac50a5c01c
commit
b0ef86224a
58
src/main.cpp
58
src/main.cpp
@ -9,6 +9,10 @@
|
||||
#include <unistd.h> // write, STDOUT_FILENO
|
||||
#include <chrono>
|
||||
|
||||
// 可通过这些开关快速启用/禁用线程进行调试
|
||||
constexpr bool ENABLE_RTSP_THREAD = true; // 设置为 false 禁用 RTSP 线程
|
||||
constexpr bool ENABLE_MQTT_THREAD = false; // 设置为 false 禁用 MQTT 线程
|
||||
|
||||
std::atomic<bool> g_running(true);
|
||||
|
||||
static void minimal_signal_handler(int signum)
|
||||
@ -55,15 +59,33 @@ int main()
|
||||
std::thread rtsp_thread;
|
||||
std::thread mqtt_thread;
|
||||
|
||||
std::thread rtsp_thread([&]()
|
||||
// 启动 RTSP 线程(如果启用)
|
||||
if (ENABLE_RTSP_THREAD)
|
||||
{
|
||||
rtsp_thread = std::thread([&]()
|
||||
{
|
||||
RTSPManager::start(g_app_config.cameras);
|
||||
rtsp_thread_exited.store(true, std::memory_order_relaxed); });
|
||||
LOG_INFO("[MAIN] RTSP thread started");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INFO("[MAIN] RTSP thread disabled by build-time toggle");
|
||||
}
|
||||
|
||||
// std::thread mqtt_thread([&]()
|
||||
// {
|
||||
// mqtt_client_thread_func();
|
||||
// mqtt_thread_exited.store(true, std::memory_order_relaxed); });
|
||||
// 启动 MQTT 线程(如果启用)
|
||||
if (ENABLE_MQTT_THREAD)
|
||||
{
|
||||
mqtt_thread = std::thread([&]()
|
||||
{
|
||||
mqtt_client_thread_func();
|
||||
mqtt_thread_exited.store(true, std::memory_order_relaxed); });
|
||||
LOG_INFO("[MAIN] MQTT thread started");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INFO("[MAIN] MQTT thread disabled by build-time toggle");
|
||||
}
|
||||
|
||||
// 等待退出信号
|
||||
while (g_running.load(std::memory_order_relaxed))
|
||||
@ -76,12 +98,14 @@ int main()
|
||||
const auto poll_interval = std::chrono::milliseconds(100);
|
||||
auto deadline = std::chrono::steady_clock::now() + max_wait;
|
||||
|
||||
// 等 RTSP 线程退出
|
||||
if (ENABLE_RTSP_THREAD)
|
||||
{
|
||||
while (!rtsp_thread_exited.load(std::memory_order_relaxed) &&
|
||||
std::chrono::steady_clock::now() < deadline)
|
||||
{
|
||||
std::this_thread::sleep_for(poll_interval);
|
||||
}
|
||||
|
||||
if (rtsp_thread.joinable())
|
||||
{
|
||||
if (rtsp_thread_exited.load(std::memory_order_relaxed))
|
||||
@ -94,14 +118,19 @@ int main()
|
||||
LOG_WARN("[MAIN] RTSP thread did not exit within timeout.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 等 MQTT 线程退出
|
||||
// 重置 MQTT 线程等待的截止时间
|
||||
deadline = std::chrono::steady_clock::now() + max_wait;
|
||||
|
||||
if (ENABLE_MQTT_THREAD)
|
||||
{
|
||||
while (!mqtt_thread_exited.load(std::memory_order_relaxed) &&
|
||||
std::chrono::steady_clock::now() < deadline)
|
||||
{
|
||||
std::this_thread::sleep_for(poll_interval);
|
||||
}
|
||||
|
||||
if (mqtt_thread.joinable())
|
||||
{
|
||||
if (mqtt_thread_exited.load(std::memory_order_relaxed))
|
||||
@ -114,16 +143,21 @@ int main()
|
||||
LOG_WARN("[MAIN] MQTT thread did not exit within timeout.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果某些线程仍未退出,强制结束进程(不会跑全局析构)
|
||||
if ((rtsp_thread.joinable() && !rtsp_thread_exited.load(std::memory_order_relaxed)) ||
|
||||
(mqtt_thread.joinable() && !mqtt_thread_exited.load(std::memory_order_relaxed)))
|
||||
// 如果有线程仍未退出,则强制终止
|
||||
bool any_failed = false;
|
||||
if (ENABLE_RTSP_THREAD && rtsp_thread.joinable() && !rtsp_thread_exited.load(std::memory_order_relaxed))
|
||||
any_failed = true;
|
||||
if (ENABLE_MQTT_THREAD && mqtt_thread.joinable() && !mqtt_thread_exited.load(std::memory_order_relaxed))
|
||||
any_failed = true;
|
||||
|
||||
if (any_failed)
|
||||
{
|
||||
LOG_ERROR("[MAIN] Threads did not exit in time. Forcing immediate termination.");
|
||||
_exit(1); // 强制退出(不会调用 atexit handlers)
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
LOG_INFO("[MAIN] Program exited cleanly.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user