How do you handle threads?

#1
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);
          }


}
0 Likes

#2

Is there a question here?

0 Likes

#3

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

0 Likes

#4

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

0 Likes

#5

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.

0 Likes

#6

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).

0 Likes

#7

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;
}

0 Likes

#8

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

#9

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:

0 Likes

#10

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.

0 Likes