T
TDC
Here a classic "DON'T DO THIS" example:
Public Class Test
Private m_ThreadBDone As Boolean
Sub LaunchThreadAndWait() '<--- called by thread A
Dim theadB as New Thread(Addressof(ThreadBProc))
theadB.Start()
While Not m_ThreadBDone
Sleep(250)
Loop
'at this point thread b is done
End Sub
Sub ThreadBProc
'do some long work
m_ThreadBDone = True
End Sub
End Class
Thread A loop waiting for the boolean m_ThreadBDone to change will not
necassarily see m_ThreadBDone become True because of memory caching.
(And no, I don't need to know how to solve that. I've got that well in
hand).
Given the first example, why isn't this classic threaded worker a
problem?
Public Class SecondTest
Sub LaunchThread() '<--- called by thread A
dim worker as new ThreadedWorker(SomeData)
Dim theadB as New Thread(Addressof(worker.DoWorkProc))
theadB.Start()
'whatever, do other stuff
End Sub
End Class
Public Class ThreadedWorker
m_SomeData
Public Sub New(SomeData) '<--- called by thread A
m_SomeData = SomeData
End Sub
Public Sub DoWorkProc() '<--- called by thread B
do work using m_SomeData
End Sub
End Class
In this example, the SomeData is only used by the threaded worker so
at first glance there is no contention. But since there is no
syncronization, couldn't the m_SomeData variable still be null due to
the same thread caching problem as the first one?
All of this memory caching/ memory barrier stuff is wild...
Tom
Public Class Test
Private m_ThreadBDone As Boolean
Sub LaunchThreadAndWait() '<--- called by thread A
Dim theadB as New Thread(Addressof(ThreadBProc))
theadB.Start()
While Not m_ThreadBDone
Sleep(250)
Loop
'at this point thread b is done
End Sub
Sub ThreadBProc
'do some long work
m_ThreadBDone = True
End Sub
End Class
Thread A loop waiting for the boolean m_ThreadBDone to change will not
necassarily see m_ThreadBDone become True because of memory caching.
(And no, I don't need to know how to solve that. I've got that well in
hand).
Given the first example, why isn't this classic threaded worker a
problem?
Public Class SecondTest
Sub LaunchThread() '<--- called by thread A
dim worker as new ThreadedWorker(SomeData)
Dim theadB as New Thread(Addressof(worker.DoWorkProc))
theadB.Start()
'whatever, do other stuff
End Sub
End Class
Public Class ThreadedWorker
m_SomeData
Public Sub New(SomeData) '<--- called by thread A
m_SomeData = SomeData
End Sub
Public Sub DoWorkProc() '<--- called by thread B
do work using m_SomeData
End Sub
End Class
In this example, the SomeData is only used by the threaded worker so
at first glance there is no contention. But since there is no
syncronization, couldn't the m_SomeData variable still be null due to
the same thread caching problem as the first one?
All of this memory caching/ memory barrier stuff is wild...
Tom