LoginSignup
0
0

More than 5 years have passed since last update.

Powershellでカウンティングセマフォ

Last updated at Posted at 2018-10-15

実装(Start-job,Wait-job,Remove-Jobを使う)


# 第一引数に実行するスクリプトブロック、第二引数に処理の上限数を受け取る関数「counting-semaphore」
# 第二引数がない場合は処理上限数は10となる
function counting-semaphore ([scriptblock]${scriptbl},${number}=10) { 

    # カウント用変数のインクリメント
    $script:counting++

    # カウント用変数が上限数に達した場合、処理終了を待つ
    switch ($script:counting) { 
        {$_ % ${number} -ne 0} {start-job $scriptbl -name "name$script:counting"}
        {$_ % ${number} -eq 0} {start-job $scriptbl -name "name$script:counting"
                    for ($i=1;$i -le ${number};$i++) {
                        wait-job   -name "name$i"
                        remove-job -name "name$i"
                    }
            $script:counting = 0
            }
    }    
}

実行例


for ($i=1;$i -le 2;$i++) {
        counting-semaphore {echo $using:i} 1

}

Start-Jobについて
https://social.technet.microsoft.com/Forums/ja-JP/6451c2da-48b0-47f9-b620-2ee889974697/startjob-12398scriptblock?forum=powershellja

汎用化するなら重複しない名前を発行する仕組みが必要

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0