优化线程退出

This commit is contained in:
Alvin-lyq 2026-04-20 09:59:02 +08:00
parent ff342c1f05
commit 4e48a403e5
3 changed files with 12 additions and 3 deletions

View File

@ -70,4 +70,6 @@ extern std::string filename;
extern int d_file;
extern Direction drive_mode;
extern TaskStatus task_status;
extern int thread_exit_flag; // 线程退出标志
#endif

View File

@ -37,6 +37,7 @@ int last_mode = 0;
Direction drive_mode = Direction::STRAIGHT_D;
TaskStatus task_status = TaskStatus::PENDING;
int thread_exit_flag = 0; // 线程退出标志0=继续运行1=需要退出
// 返回单位m
double ntzx_GPS_length(double lonti1, double lati1, double lonti2, double lati2)
@ -202,13 +203,14 @@ Direction straight_or_turn(double cur_direction, double des_direction, int thres
void PL_ProcThread()
{
usleep(70000);
thread_exit_flag = 0; // 重置退出标志
int road_pos = 0;
int des_pos = 3;
int direction_pos = 20;
auto next_time = std::chrono::steady_clock::now();
while (1)
while (!thread_exit_flag) // 检查线程退出标志
{
next_time += std::chrono::milliseconds(50);

View File

@ -110,8 +110,13 @@ class pl_node : public rclcpp::Node
}
else if (is_start == 1 && msg->task_status == 0)
{
pthread_cancel(pl_thread_t);
LOG_INFO("pl_thread_t is canceled");
thread_exit_flag = 1; // 设置线程退出标志
LOG_INFO("请求线程退出...");
// 等待线程正常退出
pthread_join(pl_thread_t, NULL);
LOG_INFO("线程已正常退出");
is_start = 0;
task_status = TaskStatus::COMPLETED;
}