多线程编程学习——condition_variable_any

condition_variable_any

  • 定义

    boost::condition_variable_any planner_cond_;
    
  • 用法

    boost::condition_variable_any planner_cond_;
    planner_thread_ = new boost::thread(boost::bind(&GlobalPlanner::planThread, this));
    

    条件判断锁

    planner_cond_.notify_one();//触发条件
    
    void GlobalPlanner::planThread(void)
    {
    ros::NodeHandle n;
    boost::unique_lock<boost::recursive_mutex> lock(planner_mutex_);
    while (n.ok())
    {
        while (!run_planner_)
        {
            planner_cond_.wait(lock);    //planner_cond_.notify_one();触发条件
        }
    
        run_planner_ = false;
    
        geometry_msgs::PoseStamped goal = nav_info_->obtainGoal();
        std::vector<geometry_msgs::PoseStamped> plan;
        bool res = makePlan(goal, plan);
    
        if (res && (!plan.empty()))
        {
            nav_info_->setGlobalPlan(plan);
            nav_info_->setNewGlobalPlanFlag(true);
            nav_info_->setNavStatus(CONTROLLING);
            LOG(WARNING) << "run global planner finished!";
        }
        else
        {
            nav_info_->setNavStatus(PLANNING);
            nav_info_->setReplanFlag(true);
            LOG(ERROR) << "run global planner failed! try again!";
        }
    }
    

你可能感兴趣的:(C++学习)