Error: "sync variables cannot currently be written"

Error: "sync variables cannot currently be written"

我的一个学生在他们的项目中遇到了严重的问题(作业是:http://turing.plymouth.edu/~kgb1013/?course=4310&project=0). They (and I) are getting a weird error when compiling with my test file, testSemaphore.chpl, available at: http://turing.plymouth.edu/~kgb1013/4310/semaphoreProject/testSemaphore.chpl。错误是这样的:

$CHPL_HOME/modules/standard/IO.chpl:3038: 错误:当前无法写入同步变量 - 首先将 readFE/readFF() 应用于这些变量

我已经让他们删除了所有方法的主体,但他们仍然遇到错误。这是他们的代码的主体:

use Time;

class Semaphore {
    var gate1$ : sync int;

    //Constructor
    proc Semaphore(){
    }

    //secondary Constructor
    proc Semaphore(givenTokens : int){
    }

    //returns the number of tokens available
    proc getNumTokens() : int {
        return 1;
    }

    //gives out a token
    proc p() {

    }

    //returns a token
    proc v() {
    }

}

他们为什么会收到此错误?

抱歉,您遇到了麻烦!事实证明,其中一些作为 Chapel 项目的 GitHub 问题会更好,但让我先解释一下发生了什么。

class 信号量包含一个同步变量字段。同时,写入 class 的默认实现是写入每个字段。所以 IO.chpl 中的错误消息试图说明它没有合理的输出同步变量的方法。例如。

var x: sync int;
writeln(x);

给出同样的错误。我认为在 Chapel 项目上提出一个 GitHub 问题是合理的,关于这个错误是多么难以理解(以及它如何不报告对你有用的行号)。请注意,我个人使用 chpl testSemaphore.chpl --print-callstack-on-error 来更好地理解错误 - 当您收到引用 internal/standard 模块的错误时,添加 --print-callstack-on-error 通常会有所帮助。

现在,对于赋值,有两种方法可以解决:

  1. 调整 testSemaphore.chpl 以不打印出 'writeln' 调用中的信号量表示。我注释掉了以 "Testing that" 开头的两个 writelns 并让它编译。
  2. 调整 class 信号量以包含一个 writeThis 来替换 compiler-generated write-each-field 默认值,如下所示:

这是一个这样的 writeThis 示例(另请参阅 The readThis, writeThis, and readWriteThis methods

class Semaphore {
  var gate1$ : sync int;
  // Other methods as before
  proc writeThis(f) {
    f <~> "sync semaphore";
  }
}