Is it safe to split an array between threads?

Is it safe to split an array between promises, as I did in the following code?

#!/usr/bin/env perl6
use v6;

sub my_sub ( $string, $len ) {
    my ( $s, $l );
    if $string.chars > $len {
        $s = $string.substr( 0, $len );
        $l = $len;
    }
    else {
        $s = $string;
        $l = $s.chars;
    }
    return $s, $l;
}

my @orig = <length substring character subroutine control elements now promise>;
my $len = 7;
my @copy;
my @length;
my $cores = 4;
my $p = @orig.elems div $cores;
my @vb = ( 0..^$cores ).map: { [ $p * $_, $p * ( $_ + 1 ) ] };
@vb[@vb.end][1] = @orig.elems;

my @promise;
for @vb -> $r {
    @promise.push: start {
        for $r[0]..^$r[1] -> $i {
            ( @copy[$i], @length[$i] ) = my_sub( @orig[$i], $len );
        }
    };
}
await @promise;
+6
source share
4 answers

It depends on how you define "array" and "generic." As for the array, there are two cases that need to be considered separately:

  • Fixed arrays (declared my @a[$size]); this includes multidimensional arrays with fixed sizes (for example, my @a[$xs, $ys]). They have the interesting property that the memory supporting them should never change.
  • ( my @a), . , .

, :

  • , , , - concurrency . " ", .
  • , - . , .
  • / ( , , , , , , ).

:

                     | Fixed size     | Variable size |
---------------------+----------------+---------------+
Read-only, non-lazy  | Safe           | Safe          |
Read/write or lazy   | Safe *         | Not safe      |

*, , , Perl 6, , , , .

, , , " " ( , ). , , , , ( , ). , .

, , , my @copy; my @length; , . , .

, , , gory.

+14

, start, , Perl 6 . .
Promises , await.

my @promise = do for @vb -> $r {

    start

      do  # to have the 「for」 block return its values

        for $r[0]..^$r[1] -> $i {
            $i, my_sub( @orig[$i], $len )
        }
}

my @results = await @promise;

for @results -> ($i,$copy,$len) {
  @copy[$i] = $copy;
  @length[$i] = $len;
}

start parallelism.
, : " , , , ".

Promise (), Thread (concurrency)

, , , , .

, - , Promise, .status, , Planned Kept Broken, .
, Promise, .


jnthn talk " Parallelism, Concurrency, Asynchrony Perl 6 ".

+5

MoarVM, , JVM ( Javascript).

  • .
  • segfault, :

$ perl6 -e 'my $i = 0; await do for ^10 { start { $i++ for ^10000 } }; say $i' 46785

, (, ) ( ).

, , . , , , , , , -, @Zoffix Znet @raiph.

+4

.

:

Jokes aside. The other answers seem to make too many assumptions about the implementation, none of which are verified by the spec.

-1
source

Source: https://habr.com/ru/post/1016988/


All Articles