线#

初始化线程类#

使用以下构造函数创建线程:

thread 构造函数创建一个线程对象,该对象带有一个回调函数,当线程被调用时该回调函数将被执行。

范围

描述

回调

先前定义的函数,将在线程调用时作为入口点执行。

// Define the function "threadPrint".
void threadPrint(){
  brain.screen.print("Printing!");
}
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Construct a thread "Thread" with the
  // thread class.
  thread Thread = thread(threadPrint);
}

当引用线程类方法时,此“Thread”对象将在整个 API 文档的所有后续示例中使用。

类方法#

获取ID()#

get_id() 方法返回踏板的 ID。

**返回:**表示线程 ID 的整数。

加入()#

join() 方法等待另一个线程完成其执行。

**返回:**无。

分离()#

detach() 方法允许线程从线程句柄执行。

**返回:**无。

可连接()#

如果线程可连接,则 joinable() 方法返回。

**返回:**表示线程是否可连接的布尔值。

打断()#

interrupt() 方法停止线程。

**返回:**无。

中断全部()#

interruptAll() 方法停止除主线程之外的所有线程。

**返回:**无。

设置优先级()#

setPriority(priority) 方法将线程的优先级设置为整数。

范围

描述

优先事项

表示线程优先级的整数。

**返回:**无。

优先事项()#

priority() 方法以整数形式返回线程的优先级。

**返回:**表示线程优先级的整数。

硬件并发()#

hardware_concurrency() 方法返回硬件支持的并发线程数。

**返回:**一个整数,表示硬件支持的并发线程数。

交换()#

该方法通过以下方式调用:

swap(x) 方法将线程 ID 与另一个指定的线程交换。

参数

描述

x

要交换到的线程。

**返回:**无。

swap(x, y) 方法交换参数中指定的两个线程。

参数

描述

x

与参数中设置的下一个线程交换的线程。

y

与参数中设置的前一个线程交换的线程。

**返回:**无。