Allows statements to be synchronized on a single expression.
SyncLock expression ...[ block ] End SyncLock
Parts
- expression
- Required. A unique collection of operators and values that yield a single result. block
- Optional. The statements that will execute in sequence. End SyncLock
- Terminates a SyncLock procedure.
Remarks
The SyncLock statement ensures that multiple threads do not execute the same statements at the same time. When the thread reaches the SyncLock block, it evaluates the expression and maintains this exclusivity until it has a lock on the object that is returned by the expression. This prevents an expression from changing values during the running of several threads, which can give unexpected results from your code.
Note The type of the expression in a SyncLock statement must be a reference type, such as a class, a module, an interface, array or delegate.
Example
Class Cache
Private Shared Sub Add(ByVal x As Object)
SyncLock GetType(Cache)
End SyncLock
End Sub
Private Shared Sub Remove(ByVal x As Object)
SyncLock GetType(Cache)
End SyncLock
End Sub
End Class
博客介绍了SyncLock语句,该语句可让语句在单个表达式上同步。它能确保多线程不同时执行相同语句,线程到达SyncLock块时会计算表达式并保持排他性,防止表达式在多线程运行时改变值,避免代码产生意外结果。
802

被折叠的 条评论
为什么被折叠?



