前言
在bash中并没有线程的概念,我们可以用多进程来模拟多线程的操作来达到同样的效果
示例代码
#!/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