Discussion on volatile

volatile double gyroNow=0;
void task1(void * para)
{
         gyroNow = getGyro();
}
int main()
{
 
    task1 = std::make_unique<pros::Task>((pros::task_fn_t)task1, nullptr, TASK_PRIORITY_DEFAULT , TASK_STACK_DEPTH_DEFAULT, "task1");
  while(1)
      std::cout<<gyroNow<<std::endl;
}

Volatile is generally used on IO devices.
If this is the case, should we use volatile?
It’s hard to test it out.
It’s just pseudocode,In fact, if volatile is added, many STL functions are useless

My computer education tells me your using gyroNow variable in separate task is not an asynchronous IO that requires volatile keyword.

My belief is that you do not need to use volatile because all V5 tasks are run on a single user CPU.

@jpearman could be so kind to comment on this assumption.

3 Likes

Volatile in c and c++ doesn’t really relate to multi threading. This is because it doesn’t make guarantees about atomicness and the needed memory barriers. It’s not something you’d typically use in Vex, as it’s aimed for more low level control. It also doesn’t really relate to the number of cores, as it’s not aimed toward multi threading.

In this particular case, you likely want c++’ s std atomic.

1 Like