简体   繁体   English

实现多线程处理时,批处理文件中“ .lock”有什么用?

[英]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 . 由于我是批处理脚本的新手 ,所以我无法理解为什么使用.lock以及为什么它小于等于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. 9>不是比较表达式,它是输出流9的重定向。
The syntax 9>= is nonsense, as the = has no meaning here as it will be dropped. 语法9>=毫无意义,因为=在这里没有意义,因为它将被删除。

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). 输出流9通常不存在,输出将为空文件“ wait1000.lock1”和“ wait1000.lock2”(在这种情况下,假设%random%为1000)。

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. FOR循环只是测试它是否可以写入同一文件,它将被阻止直到批处理文件退出并且写锁定将被释放。
And while at least one file is locked the command 9>"%lock%%%N" fails and then the goto :wait will be executed. 并且在至少一个文件被锁定的情况下,命令9>"%lock%%%N"失败,然后将执行goto :wait

Btw. 顺便说一句。 The label :Wait is missing in your sample file, 示例文件中缺少标签:Wait
it should be inserted just before the FOR-loop 应该将其插入到FOR循环之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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