| | 573 | ==== Example |
| | 574 | |
| | 575 | The following illustrates the difference between local and atomic regions. |
| | 576 | {{{ |
| | 577 | int x = 0; |
| | 578 | void thread(int tid) { |
| | 579 | $atomic { |
| | 580 | x = tid; |
| | 581 | } |
| | 582 | } |
| | 583 | int main() { |
| | 584 | $parfor (int i:1..2) thread(i); |
| | 585 | $assert(x==2); // violated |
| | 586 | } |
| | 587 | }}} |
| | 588 | The program above spawns two threads, each of which writes to shared variable `x` in an atomic block. |
| | 589 | The program has two possible executions: in one, thread 1 writes first, then thread 2 writes; in the other, thread 2 writes first, then thread 1 writes. |
| | 590 | The assertion is violated on the second execution. |
| | 591 | Running `civl verify` on this code will report a violation. |
| | 592 | |
| | 593 | {{{ |
| | 594 | int x = 0; |
| | 595 | void thread(int tid) { |
| | 596 | $local_start(); |
| | 597 | x = tid; |
| | 598 | $local_end(); |
| | 599 | } |
| | 600 | int main() { |
| | 601 | $parfor (int i:1..2) thread(i); |
| | 602 | $assert(x==2); // valid |
| | 603 | } |
| | 604 | }}} |
| | 605 | In this program, the threads write to `x` within a local region. This program has only one execution: first thread 1 executes the complete local region, then thread 2 executes the complete local region. Running `civl verify` on this code yields "all properties hold". |
| | 606 | |
| | 607 | ==== Use of `$yield` within a local region |
| | 608 | |