【问题标题】:barrier code and waiting for all thread to reach rendezvous and then enter critical section屏障代码并等待所有线程到达集合点然后进入临界区
【发布时间】:2023-03-04 19:03:01
【问题描述】:
semaphore mutex = 1;
semaphore barrier = 0;
int count = 0;
void barrier-done() {
    wait(mutex);
    count++;
    if (count < N ) {
      post(mutex);
      wait(barrier);
    }
    else {
      post(mutex);
      count = 0;
      for (int i = 1; i < N; i++) {
      post(barrier);
    }
  }
}

有人知道这段代码的问题吗?我正在尝试实现屏障代码。

【问题讨论】:

    标签: multithreading concurrency operating-system


    【解决方案1】:

    假设 N 是您期望等待屏障的线程数。

    例如 N=10,那么线程 1 到 9 将具有 if 条件,它们将等待屏障。 调用它的第 10 个线程将使该条件为 false,因为 (10 !

    我不确定您想要达到的确切情况。但是,这是我从您的代码中了解到的。可能您可能需要稍微调整一下 if 条件。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,但问题是在修复此错误后,您不能在函数“barrier-done”的名称中使用减号,代码将是正确的。

      【讨论】:

        猜你喜欢
        • 2015-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        相关资源
        最近更新 更多