Algorithm “模式问题”;障碍“;

Algorithm “模式问题”;障碍“;,algorithm,operating-system,Algorithm,Operating System,首先,请原谅我糟糕的、非常基础的英语 在阅读《信号灯小书》时,我遇到了模式障碍,为了完整起见,我很快就记住了其中的描述: “源代码中一组线程或进程的屏障意味着任何线程/进程必须在此点停止,并且在所有其他线程/进程达到此屏障之前无法继续。” 针对这类问题,作者提出以下解决方案: mutex = semaphore{1}; barrier = semaphore{0}; count = 0; // shared variable N is the number of process/th

首先,请原谅我糟糕的、非常基础的英语

在阅读《信号灯小书》时,我遇到了模式障碍,为了完整起见,我很快就记住了其中的描述:

“源代码中一组线程或进程的屏障意味着任何线程/进程必须在此点停止,并且在所有其他线程/进程达到此屏障之前无法继续。”

针对这类问题,作者提出以下解决方案:

mutex = semaphore{1};
barrier = semaphore{0};  
count = 0;   // shared variable  
N is the number of process/thread  

(1) rendezvous();          // generic code that each process/thread must 
                           // executes before encountering the barrier  
(2) mutex.wait();  
(3) count = count + 1;
(4) mutex.signal();
(5) if (count == N)
(6)     barrier.signal();
(7) barrier.wait();
(8) barrier.signal();
  
(9) proceed_to_the_end();  // generic code that each process/thread must  
                           // executes after passing the "barrier"  
为了解决这个问题,我自己设计了以下解决方案:

mutex = semaphore{1};
barrier = semaphore{0};  
count = 0;   // shared variable  
N is the number of process/thread 

(1) rendezvous();          // generic code that each process/thread must 
                           // executes before encountering the barrier  
(2) mutex.wait();  
(3) count = count + 1;
(4) mutex.signal();
(5) if (count < N)
(6)     barrier.wait();
(7) barrier.signal();
  
(9) proceed_to_the_end();  // generic code that each process/thread must  
                           // executes after passing the "barrier"  
mutex=信号量{1};
屏障=信号量{0};
计数=0;//共享变量
N是进程/线程数
(1) 集合();//每个进程/线程必须执行的通用代码
//在遇到障碍之前执行
(2) mutex.wait();
(3) 计数=计数+1;
(4) mutex.signal();
(5) 如果(计数
我比较了这两种解决方案,我认为它们是等效的,但这是我的观点,是我分析的结果,当然,它们可能是错误的: 我需要比较一下我的观点

您能帮我找出差异(如果存在)吗?
你能找到一些不同于这两种解决方案的次级方案吗


向您致意并感谢您的时间

到达屏障的最后一个线程会发出额外的信号,并在作者的版本中等待。您的版本省略了这些,这是因为作者的解决方案很好,也很好,因为在没有干预操作的计划中,这两个对没有任何影响

两个版本之间的功能区别在于,您的版本总是先发布最后一个线程,而作者的版本可以按任何顺序发布线程


作为补充说明,如果您实现了这一点,那么第5行上的计数读取最好是原子的(如果您在顺序不一致的处理器上实现了这一点,那么这些信号量操作最好具有获取/释放内存围栏)。

“。第5行上的计数读取最好是原子的。”. 当然泰