Skip to content

Commit fd4b367

Browse files
committed
Start of section on safety.
1 parent e16ef96 commit fd4b367

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/Language/concurrency.pod

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ hyper-operators, autothreading junctions?
2525
2626
=end comment
2727
28-
Additionally certain Perl features may implicitly operate in am asynchronous
28+
Additionally certain Perl features may implicitly operate in an asynchronous
2929
fashion, so in order to ensure predictable interoperation with these features
3030
user code should, where possible, avoid the lower level concurrency APIs
3131
(i.e. L<Thread> and L<Scheduler> ) and use the higher-level interfaces.
@@ -648,4 +648,23 @@ C<protect> returns whatever the code block returns.
648648
Because C<protect> will block any threads that are waiting to execute
649649
the critical section the code should be as quick as possible.
650650
651+
=head1 Safety Concerns
652+
653+
Some shared data concurrency issues are less obvious than others.
654+
For a good general write-up on this subject see this L<blog post|https://6guts.wordpress.com/2014/04/17/racing-to-writeness-to-wrongness-leads/>.
655+
656+
One particular issue of note is when container autovivification or extension
657+
takes place. When an L<Array> or a L<Hash> entry is initially assigned the
658+
underlying structure is altered and that operation is not async safe. For
659+
example, in this code:
660+
661+
my @array;
662+
my $slot := @array[20];
663+
$slot = 'foo';
664+
665+
The third line is the critical section as that is when the array is extended.
666+
The simplest fix is to use a <Lock> to protect the critical section . A
667+
possibly better fix would be to refactor the code so that sharing a container
668+
is not necessary.
669+
651670
=end pod

0 commit comments

Comments
 (0)