template<typename Var>
pros::rtos::MutexVar class

Constructors, destructors, conversion operators

template<typename... Args>
MutexVar(Args && ... args)
Creates a mutex-protected variable which is initialized with the given constructor arguments.

Public functions

std::optional<MutexVarLock<Var>> try_lock(std::uint32_t timeout)
Try to lock the mutex-protected variable.
template<typename Rep, typename Period>
std::optional<MutexVarLock<Var>> try_lock(const std::chrono::duration<Rep, Period>& rel_time)
Try to lock the mutex-protected variable.
MutexVarLock<Var> lock()
Lock the mutex-protected variable, waiting indefinitely.

Function documentation

template<typename Var> template<typename... Args>
pros::rtos::MutexVar<Var>::MutexVar(Args && ... args)

Creates a mutex-protected variable which is initialized with the given constructor arguments.

Parameters
args The arguments to provide to the Var constructor.

Example

// We create a pose class to contain all our odometry data in a single
// variable that can be protected by a MutexVar. Otherwise, we would have
// three seperate variables which could not be protected in a single 
// MutexVar
struct Pose {
  double x;
  double y;
  double heading;
}

pros::MutexVar<Pose> odom_pose(0.0, 0.0, 0.0);

void odom_task(void* param) {
  while(true) {
    Pose old_pose = *odom_pose.lock();

    Pose new_pose{0.0, 0.0, 0.0};
    
    // --- Calculate new pose for the robot here ---

    // Now that we have the new pose, we can update the global variables

    *odom_pose.take() = new_pose;
    
    delay(10);
  }
}

void chassis_task(void* param) {
  while(true) {

    Pose cur_pose = *odom_pose.take();
    
    // ---- Move the robot using the current locations goes here ----
    
    delay(10);
  }
}

void initialize() {
  odom_mutex = pros::Mutex();

  pros::Task odom_task(odom_task, "Odometry Task");
  pros::Task chassis_task(odom_task, "Chassis Control Task");
}

template<typename Var>
std::optional<MutexVarLock<Var>> pros::rtos::MutexVar<Var>::try_lock(std::uint32_t timeout)

Try to lock the mutex-protected variable.

Parameters
timeout Time to wait before the mutex becomes available, in milliseconds. A timeout of 0 can be used to poll the mutex.
Returns A std::optional which contains a MutexVarLock providing access to the protected variable if locking is successful.

Example

pros::MutexVar<Pose> odom_pose;

void my_task(void* param) {
  while(true) {
    std::optional<pros::MutexVar<Pose>> cur_pose_opt = odom_pose.try_lock(100);
    
    if(cur_pose_opt.has_value()) {
      Pose* cur_pose = **cur_pose_opt;
    }
    else {
      printf("Could not lock the mutex var!");
    }

    pros::delay(10);
  }
}

template<typename Var> template<typename Rep, typename Period>
std::optional<MutexVarLock<Var>> pros::rtos::MutexVar<Var>::try_lock(const std::chrono::duration<Rep, Period>& rel_time)

Try to lock the mutex-protected variable.

Returns A std::optional which contains a MutexVarLock providing access to the protected variable if locking is successful.

Example

pros::MutexVar<Pose> odom_pose;

void my_task(void* param) {
  while(true) {
    std::chrono::duration<int, std::milli> timeout(100);
    std::optional<pros::MutexVar<Pose>> cur_pose_opt = odom_pose.try_lock(timeout);
    
    if(cur_pose_opt.has_value()) {
      Pose* cur_pose = **cur_pose_opt;
    }
    else {
      printf("Could not lock the mutex var!");
    }

    pros::delay(10);
  }
}

template<typename Var>
MutexVarLock<Var> pros::rtos::MutexVar<Var>::lock()

Lock the mutex-protected variable, waiting indefinitely.

Returns A MutexVarLock providing access to the protected variable.

Example

pros::MutexVar<Pose> odom_pose;

void my_task(void* param) {
  while(true) {
    pros::delay(10);

    pros::MutexVarLock<Pose> cur_pose = odom_pose.lock();
    Pose cur_pose = *cur_pose;
     
    // do stuff with cur_pose
  }
}