waitgroup

package
v0.0.0-...-50b27ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 0 Imported by: 0

README

waitgroup

sync.WaitGroup решает задачу ожидания завершения всех горутин из набора.

Что нужно сделать?

Нужно написать реализацию WaitGroup, используя каналы.

Использование пакета sync в этой задаче запрещено!

type WaitGroup struct {}

func (wg *WaitGroup) Add(delta int) {}
func (wg *WaitGroup) Done() {}
func (wg *WaitGroup) Wait() {}

WaitGroup можно представлять себе как число.

  • При инициализации переменной типа WaitGroup число равно нулю.
  • Вызов Add прибавляет delta к числу.
  • Вызов Done вычитает из числа единицу.
  • Если при вызове Add или Done число становится отрицательным, происходит panic.
  • Вызов Wait при числе равном нулю -- это no-op.
  • Вызов Wait при числе большем нуля блокируется до тех пор, пока число не станет равным нулю.

WaitGroup может быть "переиспользована". После достижения нуля можно вызвать Add, заблокировав последующий Wait.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WaitGroup

type WaitGroup struct {
}

A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.

func New

func New() *WaitGroup

New creates WaitGroup.

func (*WaitGroup) Add

func (wg *WaitGroup) Add(delta int)

Add adds delta, which may be negative, to the WaitGroup counter. If the counter becomes zero, all goroutines blocked on Wait are released. If the counter goes negative, Add panics.

Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. If a WaitGroup is reused to wait for several independent sets of events, new Add calls must happen after all previous Wait calls have returned. See the WaitGroup example.

func (*WaitGroup) Done

func (wg *WaitGroup) Done()

Done decrements the WaitGroup counter by one.

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait()

Wait blocks until the WaitGroup counter is zero.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL