cowsayeternal: Add multithreading

What am I doing with my life
This commit is contained in:
Salt 2018-12-31 14:05:15 -06:00
parent 847e09e119
commit 210f7f4c83
1 changed files with 10 additions and 4 deletions

View File

@ -4,21 +4,27 @@ printf "Writing cows to temporary file \"%s\"\\n" \
"$cowsaidfile" "$cowsaidfile"
cowsay "E T E R N A L" > "$cowsaidfile" cowsay "E T E R N A L" > "$cowsaidfile"
trap_exit() { trap_exit() {
# kill "$cowthreadpid" > /dev/null 2>&1
wait "$cowthreadpid"
printf "\\n" printf "\\n"
cat "$cowsaidfile" cat "$cowsaidfile"
rm "$cowsaidfile" rm "$cowsaidfile"
} }
trap trap_exit EXIT trap trap_exit EXIT
iterations=1 # Cow writing thread
(
while true; do while true; do
# Shut up shellcheck; that's the whole point. cowsay does a full read # Shut up shellcheck; that's the whole point. cowsay does a full read
# before it writes back to the file anyway. # before it writes back to the file anyway.
# shellcheck disable=2094 # shellcheck disable=2094
cowsay -n < "$cowsaidfile" 1<> "$cowsaidfile" cowsay -n < "$cowsaidfile" 1<> "$cowsaidfile"
iterations=$(( iterations + 1 )) done
) &
cowthreadpid=$!
while true; do
# This line will only ever grow larger, so we don't need to clear it # This line will only ever grow larger, so we don't need to clear it
# before overwriting it. # before overwriting it.
printf "\\r%s iterations, %s bytes of cowsay; hit Ctrl+C to dump them" \ printf "\\r%s bytes of cowsay; hit Ctrl+C to dump them" \
"$iterations" \
"$(stat --printf="%s" "$cowsaidfile")" "$(stat --printf="%s" "$cowsaidfile")"
sleep 0.02
done done