Skip to content

Commit 020b259

Browse files
committed
[CaR Grant] Document atomicint in Numerics tut
1 parent d174ea2 commit 020b259

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

doc/Language/numerics.pod6

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ size on 32-bit machines, and how to detect when your program is running on such
527527
num32 | floating point | 32-bits
528528
num64 | floating point | 64-bits
529529
530+
atomicint | integer | sized to offer CPU-provided atomic operations. (typically 64 bits on 64-bit platforms and 32 bits on 32-bit ones)
531+
530532
=end table
531533
532534
=head2 Creating Native Numerics
@@ -703,6 +705,33 @@ f 42, my int $x; # Successfull call
703705
This way you do not have to constantly write, for example, C«$n +> 2» as C«$n +> (my int $ = 2)».
704706
The compiler knows the literal is small enough to fit to a native type and converts it to a native.
705707
708+
=head2 Atomic Operations
709+
710+
The language offers L<some operations|https://docs.perl6.org/type/atomicint>
711+
that are guaranteed to be performed atomically, i.e. safe to be executed
712+
by multiple threads without the need for locking with no risk of data races.
713+
714+
For such operations, the L<atomicint> native type is required. This type is
715+
similar to a plain native L<int>, except it is sized such that CPU-provided atomic operations can be performed upon it. On a 32-bit CPU it will typically
716+
be 32 bits in size, and on an a 64-bit CPU it will typically be 64 bits in size.
717+
718+
=begin code
719+
# !!WRONG!! Might be non-atomic on some systems
720+
my int $x;
721+
await ^100 .map: { start $x⚛++ };
722+
say $x; # OUTPUT: «98␤»
723+
724+
# RIGHT! The use of `atomicint` type guarantees operation is atomic
725+
my atomicint $x;
726+
await ^100 .map: { start $x⚛++ };
727+
say $x; # OUTPUT: «100␤»
728+
=end code
729+
730+
The similarity to C<int> is present in multi dispatch as well: an
731+
C<atomicint>, plain C<int>, and the sized C<int> variants are all considered
732+
to be the same by the dispatcher and cannot be differentiated through
733+
multi-dispatch.
734+
706735
=head1 Numeric Infectiousness
707736
708737
Numeric "infectiousness" dictates the resultant type when two numerics of different types are

0 commit comments

Comments
 (0)