Skip to content

A waitGroup waits for a collection of processes to finish.

License

Notifications You must be signed in to change notification settings

beiping96/waitGroup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

waitGroup

A waitGroup waits for a collection of processes to finish. The main process calls fun new/1 to set the number of processes to wait for. Then each of the processes runs and calls fun done/1 when finished. At the same time, fun wait/1 or fun wait/2 can be used to block until all processes have finished.

Just like sync.WaitGroup in Golang.

Usage

foo() ->
    % Create a waitGroup, set the number of processes to wait for.
    WaitGroup = waitGroup:new(3),

    % Call fun done/1 when Worker has finished. 
    F = fun(Arg) ->
            io:format("~p~n", [Arg]),
            waitGroup:done(WaitGroup)
        end,
    [spawn(F, [A]) || A <- lists:seq(0, 2)],

    % Block until all processes have finished.
    waitGroup:wait(WaitGroup),
    % OR fun wait/2 has the timeout (Milliseconds).
    waitGroup:wait(WaitGroup, 1000),

    allWorkersDone.

About

A waitGroup waits for a collection of processes to finish.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages