任务管理

Task management

关于任务的几个问题:

  1. 在所有示例中(Toit 部分中的任务)任务是在应用程序的 main 部分中“创建”的。这是否意味着不能在class中创建任务?我尝试了这个并得到了一个编译错误:
    class Receiver :
      task :: receive
      receive :
        while true :
          sleep --ms=100
          print "inside thread"
    
    ./web.toit:51:8: error: Unexpected token: ::
      task :: receive
           ^~
    ./web.toit:51:3: error: Missing body
      task :: receive
      ^~~~
    Compilation failed.
  1. 从示例中可以看出,生命周期任务与应用程序本身的生命周期重合。无论如何,在我找到机会创建任务之前,运行它,应用pause/suspend/freeze, resume, cancel/delete 任务等等。这些机会是否存在?

您可以在 class 内创建任务,但您需要在方法内或作为字段初始化程序的一部分执行此操作。但请注意,字段初始化程序无法访问 this,因此没有 this.

的方法

假设您想要接收器中的一个字段-class 包含该任务。那么你会想写:

class Receiver:
  task_ := null

  constructor:
    task_ = task:: receive

  receive:
    while true:
      sleep --ms=100
      print "inside thread"

您可以通过调用 cancel 来取消任务。一旦达到屈服点就会停止。

目前无法从外部pause/resume 执行任务。不过,根据您的反馈,我们正在讨论向其中添加此功能的可能性。

现在您可能只使用像信号量这样的同步原语。您可以将它添加到循环的开头。