Threads & Properties

  • Thread starter Thread starter Dave Taylor
  • Start date Start date
D

Dave Taylor

I'm writing a VB.NET app that controls some equipment. Some of the control
functions run on separate threads. For example the temperature control
object reads in the temperature value and adjusts the heater/cooler
accordingly and continuously loops on its own thread doing that. The temp.
control object has a property called SetPoint that can be set by another
thread. My question is, do I need to lock the writing of the value in the
property's set statement because the Execute routine (on its own thread)
will be reading the setpoint? and is it as simple as something like:

Public Class TempControl
Private _sp As Single

Public Property SetPoint() As Single
Get
Return _sp
End Get
Set (Value As Single)
SyncLock(_sp)
_sp = Value
End SyncLock
End Set
End Property

'Execute runs on its own thread, reading the _sp value and adjusting the
temperature output accordingly.
Private Sub Execute()
<do code here to control temperature>
End Sub
End Class


Thanks

Dave Taylor
 
No, you shouldn't need a SyncLock on classes with one simple variable.

*********

You would need a SyncLock if your class looked like this...
Public Class TempControl
Private _sp As Single
Private _timeStamp As Date

The reason you would need it in this case is that the thread may change _sp, but not have a chance to change _timeStamp before a read starts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top