How do you handle threads?

volatile int a =0;
volatile int b =0;
void loop ()
{ 
     while (1)
          {
               uint32_t now = pros::millis();
                a++;
               b+=a;
               mutex.give();
              pros::Task::delay_until(&now, 100);
            }
}

void f()
{
               if (joy1->get_digital(DIGITAL_A))  //if i don't pressed DIGITAL_A ,the loop() will stop?
                 {
                       mutex.take(10);                          // if out of time he will cout << last a?
                       std::cout<<a<<std::endl; 
                 }
}
void g()
{
         if (joy1->get_digital(DIGITAL_X))
                 {
                       mutex.take(10);                         
                       std::cout<<b<<std::endl; 
                 }
}
void opcontrol()
{
        while(1)
          {
               uint32_t now = pros::millis();
                   f();
                  g();
              pros::Task::delay_until(&now, 10);
          }


}

Is there a question here?

if i don’t pressed any btn , the loop ()will stop? waiting for i pressed btn?

and if f() take the mutex , g() will waitting for next loop’s mutex.give()?

Vision debugging is very, very cumbersome.
I plan to write a convenient debugging tool for students.
Vision is a separate thread whose loop time is controlled at 20 ms
But the lvgl thread cycle time is 100 ms.
I plan to use mutexes to solve the asynchronous problem.
But he always breaks down.I’m analyzing the reasons.

Not sure if your early school teachers did this, but imagine a classroom where you’re having a discussion. Only one student is allowed to speak at a time, and that student is determined by whomever is holding a designated ball. Students can request the ball. If they get the ball, they can give it up and let someone else take it.

You can think of a mutex operating in a similar manner. I’ll try to type up a more concrete example if you need it when I get to my computer (and not my phone).

i want the loop() will always running.
if i will get a or b , he will give me the latest data.

void loop ()
{ 
     while (1)
          {
               uint32_t now = pros::millis();
                bool flag=false;
                a++;
               b+=a;
               flag=true;
              pros::Task::delay_until(&now, 100);
            }
}
void f()
{  
        if(flag)
          {    
                std::cout<<a;
                last_a =a;
          }
       else 
          std::cout<<last_a;
}

The example you shared won’t compile… or at least not the way you think it will.

I like @sazrocks’s example here. It demonstrates what you had shared at some point before you edited your post…

1 Like

My home computer does not have their source code. Many of them are just pseudo-codes that I temporarily simulate. My English is not very good. Sometimes I feel that the code can be expressed more clearly.
:smile:

I tested it today, and I think pros or vex os seem to have finished thread safety.
If it only interacts with the main thread, Mutexes are not much needed.