summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIan Bertolacci <ian.bertolacci@gmail.com>2015-07-20 08:09:28 -0700
committerIan Bertolacci <ian.bertolacci@gmail.com>2015-07-20 08:09:28 -0700
commitbcc84f9c768041b7f260907d4432e12658b76626 (patch)
tree24273038c606d6e736d7fe1e64483e266fe2b910
parentd31b133e57f579ea201d4adff4ddb0e21e7d02bc (diff)
Explained atomics, moved atomics first
Felt the need to add in an explanation about atomics, then felt that (because atomics are common in language and in theory) it should go before sync and single vars. Should I explain sync and single?
-rw-r--r--chapel.html.markdown69
1 files changed, 36 insertions, 33 deletions
diff --git a/chapel.html.markdown b/chapel.html.markdown
index 6fe7c1b0..bbbf50e8 100644
--- a/chapel.html.markdown
+++ b/chapel.html.markdown
@@ -765,6 +765,42 @@ proc countdown( seconds: int ){
}
}
+// Atomic variables, common to many languages, are ones whose operations
+// occur uninterupted. Multiple threads can both modify atomic variables
+// and can know that their values are safe.
+// Chapel atomic variables can be of type bool, int, uint, and real.
+var uranium: atomic int;
+uranium.write( 238 ); // atomically write a variable
+writeln( uranium.read() ); // atomically read a variable
+// operations are described as functions, you could define your own operators.
+uranium.sub( 3 ); // atomically subtract a variable
+writeln( uranium.read() );
+var replaceWith = 239;
+var was = uranium.exchange( replaceWith );
+writeln( "uranium was ", was, " but is now ", replaceWith );
+var isEqualTo = 235;
+if uranium.compareExchange( isEqualTo, replaceWith ) {
+ writeln( "uranium was equal to ", isEqualTo,
+ " so replaced value with ", replaceWith );
+} else {
+ writeln( "uranium was not equal to ", isEqualTo,
+ " value stays the same... whatever it was" );
+}
+
+sync {
+ begin {
+ writeln( "Waiting to for uranium to be ", isEqualTo );
+ uranium.waitFor( isEqualTo );
+ writeln( "Uranium was set (by someone) to ", isEqualTo );
+ }
+
+ begin {
+ writeln( "Waiting to write uranium to ", isEqualTo );
+ countdown( 3 );
+ uranium.write( isEqualTo );
+ }
+}
+
// sync vars have two states: empty and full.
// If you read an empty variable or write a full variable, you are waited
// until the variable is full or empty again
@@ -801,39 +837,6 @@ sync {
someSingleVar$ = 5; // first and only write ever.
}
}
-
-// atomic variables can be of type bool, int, uint, and real of any size.
-var uranium: atomic int;
-uranium.write( 238 ); // atomically write a variable
-writeln( uranium.read() ); // atomically read a variable
-// operations are described as functions, you could define your own operators.
-uranium.sub( 3 ); // atomically subtract a variable
-writeln( uranium.read() );
-var replaceWith = 239;
-var was = uranium.exchange( replaceWith );
-writeln( "uranium was ", was, " but is now ", replaceWith );
-var isEqualTo = 235;
-if uranium.compareExchange( isEqualTo, replaceWith ) {
- writeln( "uranium was equal to ", isEqualTo,
- " so replaced value with ", replaceWith );
-} else {
- writeln( "uranium was not equal to ", isEqualTo,
- " value stays the same... whatever it was" );
-}
-
-sync {
- begin {
- writeln( "Waiting to for uranium to be ", isEqualTo );
- uranium.waitFor( isEqualTo );
- writeln( "Uranium was set (by someone) to ", isEqualTo );
- }
-
- begin {
- writeln( "Waiting to write uranium to ", isEqualTo );
- countdown( 3 );
- uranium.write( isEqualTo );
- }
-}
```
Who is this tutorial for?