About Mutex's question

int i=0;
pros::Mutex mutex;
void loop ()
{
uint32_t now=pros::millis();
   while(1)
       {
        mutex.take(0);
        i=pros::millis();
        i++;
      mutex.give();
       pros::Task::delay_until(&now, 20);
      }
}
static void  myTask(void *para)
{
    loop();
}
int get()
{
  mutex.take(20);
   return i;
}
void opcontrol()
{
pros::Task task1(myTask, nullptr, TASK_PRIORITY_DEFAULT , TASK_STACK_DEPTH_DEFAULT, "myTask");
    while (1)
      {
           std::cout<<get()<<std::endl;
           pros::delay(10);
      }
}

Why does this code always crash?
I want to get data stably after i++.
not i = pros:: millis ().

Well, I would start with “opcontorl” as one issue.

Haha, this is pseudocode. I wrote it directly on the forum. The actual code is very large.:rofl:

1 Like