Mutex

From ZENotes
Jump to: navigation, search

Mutex stands for Mutually Exclusive, understand it as a "grabbing" tool, when ordered, it takes control over the other thread(s), whatever they are doing. Mutex is really the first step when going Multi-Thread. See how task main() only contain the "firing" code for the other threads? then each thread contain a mutex argument, in the form of Acquire(moveMutex);


mutex moveMutex;

task move_square()
{
 while (true)
 {
   Acquire(moveMutex);
   OnFwd(OUT_AC, 75); Wait(1000);
   OnRev(OUT_C, 75); Wait(850);
   Release(moveMutex);
 }
}

task check_sensors()
{
 while (true)
 {
   if (SENSOR_1 == 1)
   {
     Acquire(moveMutex);
     OnRev(OUT_AC, 75); Wait(500);
     OnFwd(OUT_A, 75); Wait(850);
     Release(moveMutex);
   }
 }
}

task main()
{
 SetSensor(IN_1,SENSOR_TOUCH);
 Precedes(check_sensors, move_square);
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox