I need help again with parallel processing, I hope you'll understand my english
I use batch scripts for video and audio encoding.
My small application generates all batch commands for my encoding jobs.
It can also work with a queue list which is stored in a text file like this:
Each line is the path of a .bat that contains script for an encoding job.
(for each line, there is a corresponding batch file)%queue_list% wrote:D:\HD\Tests HD\queue tests\Pub 01.avs
D:\HD\Tests HD\queue tests\Pub 02.avs
D:\HD\Tests HD\queue tests\6canaux.ts.avs
Code: Select all
for /f "usebackq tokens=* delims=" %%a in (`type "%queue_list%"`) do (
set "bat_file=%%~dpna.bat"
setlocal EnableDelayedExpansion
call "%%bat_file%%"
:: some code...
endlocal
)
>"%queue_list%" type nul
Now, I'd like to run several jobs simultaneously (2 is enough for now) because sometimes, encoding jobs uses less than half cpu.
So I had a menu item to my app: "run a new worker" which calls the same batch script.
The 2 process have to share a counter %bat_cnt% which represent the last line number that is being processed.
Each time a job is finished, the process calls the counter and increments its value.
I only know 2 solutions for this: using environnement variables or using a temporary counter.bat
I choose the second one because I think it is faster:
Code: Select all
:: read counter
call "%queue_cnt_path%"
:: update counter
>"%queue_cnt_path%" echo set bat_cnt=!num_bat!
Now the problem:
I don't know how many chance I have it happens, but 2 process can read the value of the counter exactly at the same time (especially with fast tasks) and I don't know how to avoid this.
If this happens, the 2 process will do the same encoding job at the same time
I know batch is not the best for multitask but if you have ideas that could help, please share.
Thanks you very much!