Name

SPVM::Go::Sync::WaitGroup - Go WaitGroup

Description

The Go::Sync::WaitGroup class in SPVM has methods to manipulate waiting count.

Usage

use Go::Sync::WaitGroup;
use Fn;

Go->go(method : void () {
  my $wg = Go::Sync::WaitGroup->new;
  
  $wg->add(2);
  
  Go->go([has wg : Go::Sync::WaitGroup = $wg] method : void () {
    
    Fn->defer([has wg : Go::Sync::WaitGroup = $self->{wg}] method : void () {
      $self->{wg}->done;
    });
    
  });
  
  Go->go([has wg : Go::Sync::WaitGroup = $wg] method : void () {
    Fn->defer([has wg : Go::Sync::WaitGroup = $self->{wg}] method : void () {
      $self->{wg}->done;
    });
    
  });
  
  $wg->wait;
});

Go->gosched;

Fields

count

has count : int;

The count of waiting.

Class Methods

new

static method new : Go::Sync::WaitGroup ();

Creates a new Go::Sync::WaitGroup object.

Instance Methods

add

method add : void ($delta : int = 1);

Adds $delta to the count of waiting.

This method is thread-safe.

Exceptions:

The count field must be greater than or equal to 0. Otherwise an exception is thrown.

done

method done : void ();

Decrements the count of waiting.

The same as

$wait_group->add(-1);

This method is thread-safe.

wait

method wait : void ();

Waits until the count of waiting is 0.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License