You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/numerics.pod6
+29Lines changed: 29 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -527,6 +527,8 @@ size on 32-bit machines, and how to detect when your program is running on such
527
527
num32 | floating point | 32-bits
528
528
num64 | floating point | 64-bits
529
529
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
+
530
532
=endtable
531
533
532
534
=head2Creating Native Numerics
@@ -703,6 +705,33 @@ f 42, my int $x; # Successfull call
703
705
This way you do not have to constantly write, for example, C«$n +> 2» as C«$n +> (my int $ = 2)».
704
706
The compiler knows the literal is small enough to fit to a native type and converts it to a native.
705
707
708
+
=head2Atomic 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
+
=begincode
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
+
=endcode
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
+
706
735
=head1Numeric Infectiousness
707
736
708
737
Numeric "infectiousness" dictates the resultant type when two numerics of different types are
0 commit comments