滤波器锁定算法 [英] Filter Lock Algorithm

查看:150
本文介绍了滤波器锁定算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙着查看用于n线程互斥的过滤器锁定算法,我似乎无法理解代码的第17行。我知道它在某种条件下旋转但不完全确定这些条件是什么。更具体地说(∃k!= me)需要什么。

I'm busy looking at the filter lock algorithm for n-thread mutual exclusion and I can't seem to understand line 17 of the code. I understand that it is spinning on a condition but not entirely sure what those conditions are. More specifically what (∃k != me) entails.

1 class Filter implements Lock {
2 int[] level;
3 int[] victim;
4 public Filter(int n) {
5     level = new int[n];
6     victim = new int[n]; // use 1..n-1
7     for (int i = 0; i < n; i++) {
8         level[i] = 0;
9     }
10 }
11 public void lock() {
12     int me = ThreadID.get();
13     for (int i = 1; i < n; i++) { //attempt level 1
14     level[me] = i;
15     victim[i] = me;
16     // spin while conflicts exist
17     while ((∃k != me) (level[k] >= i && victim[i] == me)) {};
18     }
19 }
20 public void unlock() {
21     int me = ThreadID.get();
22     level[me] = 0;
23 }
24 }


推荐答案

我的阅读

(∃k != me) (level[k] >= i && victim[i] == me)

是存在一些 k me 以外的等级[k]> = i&& victim [i] == me

循环旋转,直到条件成立的 k 为止。

The loop spins until there is no k for which the condition holds.

以下是另一种陈述相同内容的方法:

Here is another way to state the same thing:

boolean conflicts_exist = true;
while (conflicts_exist) {
   conflicts_exist = false;
   for (int k = 1; k < n; k++) {
      if (k != me && level[k] >= i && victim[i] == me) {
         conflicts_exist = true;
         break;
      }
   }
}

这篇关于滤波器锁定算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆