Trapos#

Introducción#

Los subprocesos permiten que un robot ejecute múltiples tareas simultáneamente dentro del mismo programa. Facilitan la multitarea, permitiendo que el robot realice acciones independientes simultáneamente.

Class Constructors#

1 Creates and starts a new thread with a function.

thread( void (* callback)(void) );

2 Creates and starts a new thread with a function that accepts an argument.

thread( int (* callback)(void *), void *arg );

Class Destructor#

Destroys the thread object and releases associated resources.

~thread();

Parámetros#

Parameter

Type

Descripción

callback

void (*)(void)

A previously defined function that will run in the new thread.

callback

int (*)(void *)

A previously defined function that accepts a void pointer argument and will run in the new thread.

arg

void *

A void pointer that is passed to the callback function.

Notes#

  • A function must always be defined before it is called.

  • When you create a thread, you can name it to manage it individually in your project.

  • Once a thread is stopped using interrupt(), it cannot be restarted. To run it again, you must create a new thread.

Example#

// Drive forward while blinking screen
void blinkScreen() {
  while (true) {
    Brain.Screen.clearScreen(red);
    wait(0.5, seconds);
    Brain.Screen.clearScreen();
    wait(0.5, seconds);
  }
}

// Begin project code
thread blinkScreenThread = thread(blinkScreen);
Drivetrain.drive(forward);

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

// Begin project code
thread blinkScreenThread = thread(blinkScreen);
thread turningThread = thread(turning);

Member Functions#

The thread class includes the following member functions:

  • interrupt – Stops a thread manually, useful for halting background behavior.

interrupt#

Stops a thread manually, which is useful when a task is no longer needed or when a program needs to reset or reassign threads.

Available Functions
void interrupt();

Parameters

This function does not have parameters.

Return Values

This function does not return a value.

Notes
  • Once a thread is stopped, it cannot be restarted. To run it again, you must create a new thread using thread.

  • This is useful when a task is no longer needed or when a program needs to reset or reassign threads.

Examples
// 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);
  }
}

// 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();