실#

소개#

스레드를 사용하면 로봇이 동일한 프로그램 내에서 여러 작업을 동시에 실행할 수 있습니다. 이를 통해 멀티태스킹이 가능해지며, 로봇이 동시에 독립적인 동작을 수행할 수 있습니다.

클래스 생성자#

1 — 함수를 사용하여 새 스레드를 생성하고 시작합니다.

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

2 — 인수를 받는 함수를 사용하여 새 스레드를 생성하고 시작합니다.

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

클래스 소멸자#

thread 번 객체를 파괴하고 관련 리소스를 해제합니다.

~thread();

매개변수#

매개변수

유형

설명

callback

void (*)(void)

새 스레드에서 실행될 미리 정의된 함수입니다.

callback

int (*)(void *)

이전에 정의된 함수로, void 포인터 인수를 받으며 새 스레드에서 실행됩니다.

arg

void *

콜백 함수에 전달되는 void 포인터입니다.

메모#

  • 함수는 호출되기 전에 반드시 정의되어 있어야 합니다.

  • 스레드를 생성할 때 이름을 지정하여 프로젝트에서 개별적으로 관리할 수 있습니다.

  • interrupt()를 사용하여 스레드를 중지하면 다시 시작할 수 없습니다. 다시 실행하려면 새 스레드를 생성해야 합니다.

예#

함수를 정의하십시오 ( 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);
  }
}

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

함수를 정의하십시오 ( 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);
}

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

회원 기능#

스레드 클래스는 다음과 같은 멤버 함수를 포함합니다.

  • interrupt – 스레드를 수동으로 중지합니다. 백그라운드 동작을 중지하는 데 유용합니다.

interrupt#

스레드를 수동으로 중지합니다. 이는 작업이 더 이상 필요하지 않거나 프로그램이 스레드를 재설정해야 할 때 유용합니다.

Available Functions
void interrupt();

Parameters

이 함수는 매개변수를 받지 않습니다.

Return Values

이 함수는 값을 반환하지 않습니다.

Notes
  • 스레드가 중지되면 다시 시작할 수 없습니다. 다시 실행하려면 thread를 사용하여 새 스레드를 생성해야 합니다.

  • 이 기능은 작업이 더 이상 필요하지 않거나 프로그램이 스레드를 재설정하거나 재할당해야 할 때 유용합니다.

Examples

함수를 정의하십시오 ( 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);
  }
}

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