-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwait_group_ex
executable file
·43 lines (35 loc) · 1.39 KB
/
wait_group_ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
#
# Example similar to the one in Go documentation
# (https://pkg.go.dev/sync#example-WaitGroup).
readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${DIR}/../gobash
function main() {
# This function illustrates how we create and use
# WaitGroup. However, this example does not really need
# WaitGroup as we wait for all sub processes; we can simply
# use wait command at the end of the function to wait for
# every process that we spawned. The value of this example is to
# illustrate how to create a WaitGroup, processes to
# it, and wait (which would be a nice approach if one has to
# wait only for a subset of processes).
local -r wg=$(WaitGroup)
# If values are needed only locally (not passing/returning
# to/from functions/processes), we use array structure
# available in bash.
local -r urls=(
"http://www.golang.org/"
"http://www.google.com/"
"http://www.example.com/"
)
local url
for url in ${urls[@]}; do
( curl "${url}" 2>&1 ) &
# Just like the wait command, it is only legal to wait
# for sub processes (i.e., you should not be adding
# random values to a WaitGroup).
$wg add $!
done
$wg wait
}
main