first commit

This commit is contained in:
cxh 2025-09-10 13:03:16 +08:00
parent ac50a5c01c
commit b0ef86224a

View File

@ -9,6 +9,10 @@
#include <unistd.h> // write, STDOUT_FILENO #include <unistd.h> // write, STDOUT_FILENO
#include <chrono> #include <chrono>
// 可通过这些开关快速启用/禁用线程进行调试
constexpr bool ENABLE_RTSP_THREAD = true; // 设置为 false 禁用 RTSP 线程
constexpr bool ENABLE_MQTT_THREAD = false; // 设置为 false 禁用 MQTT 线程
std::atomic<bool> g_running(true); std::atomic<bool> g_running(true);
static void minimal_signal_handler(int signum) static void minimal_signal_handler(int signum)
@ -55,15 +59,33 @@ int main()
std::thread rtsp_thread; std::thread rtsp_thread;
std::thread mqtt_thread; std::thread mqtt_thread;
std::thread rtsp_thread([&]() // 启动 RTSP 线程(如果启用)
{ if (ENABLE_RTSP_THREAD)
RTSPManager::start(g_app_config.cameras); {
rtsp_thread_exited.store(true, std::memory_order_relaxed); }); 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 线程(如果启用)
// { if (ENABLE_MQTT_THREAD)
// mqtt_client_thread_func(); {
// mqtt_thread_exited.store(true, std::memory_order_relaxed); }); 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)) while (g_running.load(std::memory_order_relaxed))
@ -76,54 +98,66 @@ int main()
const auto poll_interval = std::chrono::milliseconds(100); const auto poll_interval = std::chrono::milliseconds(100);
auto deadline = std::chrono::steady_clock::now() + max_wait; 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); while (!rtsp_thread_exited.load(std::memory_order_relaxed) &&
} std::chrono::steady_clock::now() < deadline)
if (rtsp_thread.joinable())
{
if (rtsp_thread_exited.load(std::memory_order_relaxed))
{ {
rtsp_thread.join(); std::this_thread::sleep_for(poll_interval);
LOG_INFO("[MAIN] RTSP thread finished and joined.");
} }
else
if (rtsp_thread.joinable())
{ {
LOG_WARN("[MAIN] RTSP thread did not exit within timeout."); if (rtsp_thread_exited.load(std::memory_order_relaxed))
{
rtsp_thread.join();
LOG_INFO("[MAIN] RTSP thread finished and joined.");
}
else
{
LOG_WARN("[MAIN] RTSP thread did not exit within timeout.");
}
} }
} }
// 等 MQTT 线程退出 // 重置 MQTT 线程等待的截止时间
deadline = std::chrono::steady_clock::now() + max_wait; deadline = std::chrono::steady_clock::now() + max_wait;
while (!mqtt_thread_exited.load(std::memory_order_relaxed) &&
std::chrono::steady_clock::now() < deadline) if (ENABLE_MQTT_THREAD)
{ {
std::this_thread::sleep_for(poll_interval); while (!mqtt_thread_exited.load(std::memory_order_relaxed) &&
} std::chrono::steady_clock::now() < deadline)
if (mqtt_thread.joinable())
{
if (mqtt_thread_exited.load(std::memory_order_relaxed))
{ {
mqtt_thread.join(); std::this_thread::sleep_for(poll_interval);
LOG_INFO("[MAIN] MQTT thread finished and joined.");
} }
else
if (mqtt_thread.joinable())
{ {
LOG_WARN("[MAIN] MQTT thread did not exit within timeout."); if (mqtt_thread_exited.load(std::memory_order_relaxed))
{
mqtt_thread.join();
LOG_INFO("[MAIN] MQTT thread finished and joined.");
}
else
{
LOG_WARN("[MAIN] MQTT thread did not exit within timeout.");
}
} }
} }
// 如果某些线程仍未退出,强制结束进程(不会跑全局析构) // 如果有线程仍未退出,则强制终止
if ((rtsp_thread.joinable() && !rtsp_thread_exited.load(std::memory_order_relaxed)) || bool any_failed = false;
(mqtt_thread.joinable() && !mqtt_thread_exited.load(std::memory_order_relaxed))) 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."); LOG_ERROR("[MAIN] Threads did not exit in time. Forcing immediate termination.");
_exit(1); // 强制退出(不会调用 atexit handlers _exit(1);
} }
LOG_INFO("[MAIN] Program exited cleanly."); LOG_INFO("[MAIN] Program exited cleanly.");
return 0; return 0;
} }