i) 有一些测试代码,每载一页,载于我的印本网网站。
这是法典。
Sub TryThreads()
Dim t1 = New Thread(AddressOf TryLock)
t1.Priority = ThreadPriority.Lowest
t1.Start()
Dim t2 = New Thread(AddressOf TryLock)
t2.Priority = ThreadPriority.Lowest
t2.Start()
End Sub
Sub TryLock()
Dim LockObject = New Object
SyncLock LockObject
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
“race”只是在 d的原木表中添加一个记录。
现在,正确的结果是,如果是“进入”、“退出”、“退出”的,就应当有 d条目。
but actually when i look in the db i see first 2 "entered" then 2 "exiting" etc. meaning that the multithreading is working ok, but not the synclock is that correct?
and how can this be fixed? the real code will be adding records to the db and might be called from several pages of different sessions, but the same code must not run twice concurrently
i 赞赏任何人的帮助
非常感谢!
http://www.un.org。
作为对萨沙奇的教职,一把我的法典改为一等(正处在一个单元),现在它喜欢:
Public Class CheckClass
Property LockObject As Object
Get
If HttpRuntime.Cache("CheckSessionsLock") Is Nothing Then HttpRuntime.Cache("CheckSessionsLock") = New Object
Return HttpRuntime.Cache("CheckSessionsLock")
End Get
Set(ByVal value As Object)
If value Is Nothing Then
HttpRuntime.Cache.Remove("CheckSessionsLock")
Else
HttpRuntime.Cache("CheckSessionsLock") = value
End If
End Set
End Property
Sub TryThreads()
Dim t1 = New Thread(AddressOf TryLock)
t1.Priority = ThreadPriority.Lowest
t1.Start()
Dim t2 = New Thread(AddressOf TryLock)
t2.Priority = ThreadPriority.Lowest
t2.Start()
End Sub
Sub TryLock()
SyncLock LockObject
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
End Class
now it works 80-90% of the time. on page load i have:
Dim cc = New CheckClass
cc.TryThreads()
如果有一页多页,它们仍会发生冲突。 但如果一刀切,现在问题就好像在网上。 cache,因为当使用标准财产时,该法典在一页运行100%。
这样,我怎么能够确保两条read子,即使完全不同的会议也永远不会同时驾 the?
感谢大家帮助