Trapos#
Introducción#
Los hilos permiten que un robot ejecute varias tareas simultáneamente dentro del mismo programa. Facilitan la multitarea, permitiendo que el robot realice acciones independientes al mismo tiempo.
Constructores de clases#
1 — Crea e inicia un nuevo hilo con una función.
thread( void (* callback)(void) );
2 — Crea e inicia un nuevo hilo con una función que acepta un argumento.
thread( int (* callback)(void *), void *arg );
Instructor de clase#
Destroys the thread object and releases associated resources.
~thread();
Parámetros#
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Una función definida previamente que se ejecutará en el nuevo hilo. |
|
|
Una función definida previamente que acepta un argumento de puntero void y se ejecutará en el nuevo hilo. |
|
|
Un puntero void que se pasa a la función de devolución de llamada. |
Notas#
Una función debe **definirse siempre antes de ser llamada.
Al crear un hilo, puedes asignarle un nombre para gestionarlo individualmente en tu proyecto.
Once a thread is stopped using
interrupt(), it cannot be restarted. To run it again, you must create a new thread.
Ejemplo#
Define the function (outside of
int main())// Drive forward while blinking screen void blinkScreen() { while (true) { Brain.Screen.clearScreen(red); wait(0.5, seconds); Brain.Screen.clearScreen(); wait(0.5, seconds); } }Register the thread inside
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code thread blinkScreenThread = thread(blinkScreen); Drivetrain.drive(forward); }
Define the function (outside of
int main())// Run multiple threads simultaneously // Turn right, blink screen at once void blinkScreen() { while (true) { Brain.Screen.clearScreen(red); wait(0.5, seconds); Brain.Screen.clearScreen(); wait(0.5, seconds); } } void turning() { Drivetrain.turn(right); }Register the thread inside
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code thread blinkScreenThread = thread(blinkScreen); thread turningThread = thread(turning); }
Funciones de los miembros#
La clase Thread incluye las siguientes funciones miembro:
interrupt– Stops a thread manually, useful for halting background behavior.
interrupt#
Detiene un hilo manualmente, lo cual resulta útil cuando una tarea ya no es necesaria o cuando un programa necesita reiniciar o reasignar hilos.
Available Functionsvoid interrupt();
Esta función no tiene parámetros.
Return ValuesEsta función no devuelve ningún valor.
NotesUna vez que se detiene un hilo, no se puede reiniciar. Para volver a ejecutarlo, debe crear un nuevo hilo usando
thread.Esto resulta útil cuando una tarea ya no es necesaria o cuando un programa necesita reiniciarse o reasignar subprocesos.
Define the function (outside of
int main())// Loop through red and green void colorLoop() { while (true) { Brain.Screen.drawRectangle(0, 120, 480, 120, red); wait(0.5, seconds); Brain.Screen.drawRectangle(0, 120, 480, 120, green); wait(0.5, seconds); } } // Display the current heading void displayHeading() { Brain.Screen.setFont(prop60); while (true) { Brain.Screen.clearLine(1); Brain.Screen.setCursor(1, 1); Brain.Screen.print( "Heading: %.1f", DrivetrainInertial.heading(degrees) ); wait(50, msec); } }Register the thread inside
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Turn right, change screen color, and display heading at once thread colorThread = thread(colorLoop); thread displayHeadingThread = thread(displayHeading); Drivetrain.turn(right); wait(2, seconds); // Stop the flashing colors colorThread.interrupt(); }