While the loop in the method is stuck. Adding a field job for yourself fixes the problem

By running our Semaphore project, I gave my students a bad version of the p () method:

proc p() { while (this.tokens <= 0) { sleep(1); writeln("Tokens: ", this.tokens); } this.tokens -= 1; } 

I give them additional code to test it, which increases the number of tokens (using the v () method) in another thread. The number of tokens increases (above 0), but the code does not exit the while loop.

On a whim, I added an assignment operator:

 proc p() { while (this.tokens <= 0) { this.tokens = this.tokens; sleep(1); writeln("Tokens: ", this.tokens); } this.tokens -= 1; } 

This solves the problem in test runs (although this is even less thread safe). Why is the original while loop stuck and why does adding this assignment resolve it?

+5
source share
1 answer

Assuming p() and v() called from different tasks, there is no guarantee that writing to the non- this.tokens in one task will ever be considered by another task.

One solution would be to make tokens atomic and have something like:

 proc p() { while (this.tokens.read() <= 0) { sleep(1); writeln("Tokens: ", this.tokens.read()); } this.tokens.sub(1); } 

When tokens not atomic, the compiler may assume that tokens will not be modified by other tasks, so it will probably convert your code to something like:

 var tokenTemp = this.token; while (tokenTemp <= 0) ... 

and inserting a record in token prevents the ascent. However, even with token writing, this is still invalid / illegal / undefined code and can easily be handled by some reordering of the compiler / processor in the future.

This code is illegal because it violates the clock memory consistency model (MCM). In particular, it is a data race, and Chapel provides only consistent consistency for race-free programs.

The memory consistency model is defined in the language specification (chapter 30 at https://chapel-lang.org/docs/1.16/_downloads/chapelLanguageSpec.pdf ). It has a nice and fairly affordable introduction paragraph. However, since this is a language specification, the rest of the chapter is pretty dry and technical, so this is probably not the best place for developers to learn.

For a shorter review, take a look at https://chapel-lang.org/CHIUW/2015/hot-topics/01-Ferguson.pdf (especially slide 10).

In addition, the Chapel memory model is based on C11 / C ++ 11, Java, UPC and others. There are plenty of great and affordable articles there if you're looking for a โ€œC ++ 11 memory model,โ€ โ€œa free data feed program,โ€ or โ€œconsistent consistency.โ€

+4
source

Source: https://habr.com/ru/post/1275275/


All Articles