daichitakahashi / oncewait

Package oncewait offers slightly more synced sync.Once and its factory.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

oncewait

Go Reference

Package oncewait offers slightly more synced sync.Once and its factory.

Usage

func ExampleOnceWaiter(){
    once := oncewait.New()

    processFunc := func() {
        fmt.Println("start heavy process.")

        time.Sleep(time.Second) // do something

        fmt.Println("process finished.")
    }

    var wg sync.WaitGroup

    wg.Add(1)
    go func() {
        once.Do(processFunc)
        fmt.Println("done.")
        wg.Done()
    }()

    wg.Add(1)
    go func() {
        once.Do(processFunc)
        fmt.Println("done.")
        wg.Done()
    }()

    wg.Wait()

    // Output:
    // start heavy process.
    // process finished.
    // done.
    // done.
}

About

Package oncewait offers slightly more synced sync.Once and its factory.

License:MIT License


Languages

Language:Go 100.0%