Multithreading in Bash [duplicate]
Sure, just add &
after the command:
read_cfg cfgA &
read_cfg cfgB &
read_cfg cfgC &
wait
all those jobs will then run in the background simultaneously. The optional wait
command will then wait for all the jobs to finish.
Each command will run in a separate process, so it's technically not "multithreading", but I believe it solves your problem.
You can run several copies of your script in parallel, each copy for different input data, e.g. to process all *.cfg files on 4 cores:
ls *.cfg | xargs -P 4 -n 1 read_cfg.sh
The read_cfg.sh script takes just one parameters (as enforced by -n)
Bash job control involves multiple processes, not multiple threads.
You can execute a command in background with the &
suffix.
You can wait for completion of a background command with the wait
command.
You can execute multiple commands in parallel by separating them with |
. This provides also a synchronization mechanism, since stdout of a command at left of |
is connected to stdin of command at right.