Bash script as much as threading

Foreword

In bash and no concept of threads, we can use multiple processes to simulate the operation of multi-threading to achieve the same effect

Sample Code

#!/bin/bash

# thread number can modify if needed
THREAD=10
ROOTDIR="$(pwd)"
TMP_FIFO="/tmp/$.fifo"

#cannot del here. this is thread control
mkfifo "${TMP_FIFO}"
exec 6<>"${TMP_FIFO}"
rm ${TMP_FIFO}

needToDo(){
#do something her you need mutithread do
}

#cannot del here. this is thread control
for ((i=0;i<${THREAD};i++));
do
    echo
done >&6

while ((1)) #some control 
do
    read -u6
    {
        needToDo
        echo >&6
    }&
done 


wait
exec 6>&-
exit 0

Published 15 original articles · won praise 13 · views 60000 +

Guess you like

Origin blog.csdn.net/qq410942197/article/details/104004250