@echo off setlocal EnableDelayedExpansion rem Multi-thread scheme in Batch files rem Antonio Perez Ayala aka Aacini set "myID=%~2" if "%~1" neq "" goto %1 set "signal=X" for /L %%i in (1,1,10) do set "signal=!signal!!signal!" rem Create the list of start commands for the concurrent working threads set "numThreads=%NUMBER_OF_PROCESSORS%" set "threads=" del availableThread* 2> NUL for /L %%i in (1,1,%numThreads%) do ( set "threads=!threads! & start "" /B cmd /C "%~NX0" Thread %%i" > availableThread%%i echo %%i ) echo Start of application del requestForThread* 2> NUL ( %threads:~3% ) | "%~NX0" Main echo End of application goto :EOF :Main echo Main - started rem Controlling code: process all files and assign each one to available threads for /F "delims=" %%f in ('dir /A-D /B *.txt') do ( rem Get the number of next available thread; waits for anyone, if necessary if not exist availableThread* ( echo X > mainIsWaiting echo Main - waiting for an available thread set /P availableSignal= del mainIsWaiting ) set "nextThread=" for %%t in (availableThread*) do if not defined nextThread ( set /P "nextThread=" < %%t del %%t ) if not defined nextThread ( echo SYNCHRO FAILED^^! goto waitThreadsEnds ) rem Assign the next file to the available thread echo %%f> requestForThread!nextThread! echo Main - file "%%f" assigned to thread #!nextThread! ) echo Main - all files sent to processing rem Wait until all threads ends their tasks :waitThreadsEnds set availableThreads=0 for %%t in (availableThread*) do set /A availableThreads+=1 if %availableThreads% lss %numThreads% goto :waitThreadsEnds del availableThread* rem Send "exit" signals to all threads for /L %%i in (1,1,%numThreads%) do echo exit> requestForThread%%i echo Main - end goto :EOF :Thread rem Wait until a request for this thread appear if not exist requestForThread%myID% goto Thread set "request=" set /P "request=" < requestForThread%myID% if not defined request goto Thread del requestForThread%myID% if "%request%" equ "exit" goto :EOF rem Process the file echo %time% - Thread #%myID% start: "%request%" > CON set /A seconds=6+%random:~-1% ping localhost -n %seconds% > NUL echo %time% - Thread #%myID% end: "%request%" > CON rem Inform that this thread is available for other task > availableThread%myID% echo %myID% if exist mainIsWaiting echo %signal:~0,1021% goto Thread |