指定のオブジェクトの相互排他ロックを取得し、文のブロックを実行してロックを解放することで、文ブロックを重要なセクションとしてマークします。
例
class-id Account.
working-storage section.
01 balance binary-long.
01 r type Random value new Random.
method-id new.
procedure division using value init as binary-long.
move init to balance
end method.
method-id Withdraw.
procedure division using value amount as binary-long returning ret as binary-long.
*> この条件は、ロック文が
*> コメント アウトされている場合を除き、絶対に true にはならない
if balance > 0
display "< 0"
raise new Exception("Negative Balance")
end-if
sync on self
if balance >= amount
display "In critical section"
*>display "Balance before Withdrawal : " balance を表示する
*>display "Amount to Withdraw : -" amount を表示する
compute balance = balance - amount
*>display "Balance after Withdrawal : " balance を表示する
move amount to ret
else
display "In critical section"
move 0 to ret
end-if
end-sync
end method.
method-id DoTransactions.
local-storage section.
01 i binary-long.
procedure division.
perform varying i from 0 by 1 until i = 100
invoke self::Withdraw(r::Next(1, 100))
end-perform
end method.
end class.