From bcc84f9c768041b7f260907d4432e12658b76626 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 20 Jul 2015 08:09:28 -0700 Subject: 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? --- chapel.html.markdown | 69 +++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 33 deletions(-) (limited to 'chapel.html.markdown') 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? -- cgit v1.2.3