简体   繁体   中英

What is the use of “.lock” in batch file while implementing multithreading?

Here is the code. As I am new to batch script I cannot understand why .lock is used and why it is less than equal to 9 .

set "lock=%temp%\wait%random%.lock"

start "" cmd /c 9>="%lock%1" abcd.bat 4441 %tempdate%

start "" cmd /c 9>="%lock%2" pqrs.bat 4442 %tempdate%

for %%N in (1 2 3 4 5 6 7 8 9) do (

        9>="%lock%%%N" || goto :Wait

) 2>nul

9> isn't a compare expression, it's a redirection of the output stream 9.
The syntax 9>= is nonsense, as the = has no meaning here as it will be dropped.

Output stream 9 normally doesn't exist, the output will be empty files "wait1000.lock1" and "wait1000.lock2" (assuming %random% is 1000 in this case).

The FOR loop simply tests if it can write to the same file, this will be blocked until the batch files exits and the write lock will be released.
And while at least one file is locked the command 9>"%lock%%%N" fails and then the goto :wait will be executed.

Btw. The label :Wait is missing in your sample file,
it should be inserted just before the FOR-loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM