Hi
I have a dictionary object (let's call it _do) I am using to cache some values for use in a multi-threaded program. I have a single controlling parent thread, with a timer function _doTimer() which can kick off child threads with a new instance of class MyThreadObject in each. My parent thread has _do as a property and with each time _doTimer() gets called, it may or may not repopulate _do depending on cacheflag settings. I pass a reference to _do to each instance of MyThreadObject and those threads at some point read the values of _do but DO NOT write to it.
What I want is to ensure that the child threads do not try and read _do whilst the parent thread is writing to it. Same thing but very slightly different -> I also don't want the parent thread to write to _do whilst the child threads are reading from it. However there is no problem with 1 child thread reading the values of _do at the same time as another child thread is reading the values - so ideally I do not want each child thread to completely lock out read access, only locking out Write access..
The code is roughly like this:
I'm kind of thinking that the only way I can do this would be to only ever allow singular (SyncLocked) access to either read from or write to the _do variable. This is not ideal though as it would hold up the MyThreadObject objects unnecessarily.
Any help very gratefully appreciated!
Gareth
I have a dictionary object (let's call it _do) I am using to cache some values for use in a multi-threaded program. I have a single controlling parent thread, with a timer function _doTimer() which can kick off child threads with a new instance of class MyThreadObject in each. My parent thread has _do as a property and with each time _doTimer() gets called, it may or may not repopulate _do depending on cacheflag settings. I pass a reference to _do to each instance of MyThreadObject and those threads at some point read the values of _do but DO NOT write to it.
What I want is to ensure that the child threads do not try and read _do whilst the parent thread is writing to it. Same thing but very slightly different -> I also don't want the parent thread to write to _do whilst the child threads are reading from it. However there is no problem with 1 child thread reading the values of _do at the same time as another child thread is reading the values - so ideally I do not want each child thread to completely lock out read access, only locking out Write access..
The code is roughly like this:
Code:
Public Class ParentThread
Dim _do as Dictionary(Of String, Integer)
Dim timer1 As New System.Timers.Timer
Public Sub Start()
timer1.Enabled = True
timer1.Interval = 10
AddHandler timer1.Elapsed, AddressOf dotimer
End Sub
Public Sub dotimer()
if staticFunctions.refreshCache() = true then
'// Some kind of SyncLock wrapper here???
_do = staticFunctions.GetNewValues()
end if
Dim workerthread As New MyThreadObject()
workerthread._doCache = _do
Dim new_worker As New Thread(AddressOf workerthread.doStuff)
new_worker.Start()
End Sub
End Class
Public Class MyThreadObject
Public _doCache as Dictionary(Of String, Integer)
Public Sub doStuff()
'// Some kind of SyncLock here??
For Each kvp As KeyValuePair(Of String, String) In doCache
'// Do stuff based on values in kvp...
Next kvp
End Sub
End Class
I'm kind of thinking that the only way I can do this would be to only ever allow singular (SyncLocked) access to either read from or write to the _do variable. This is not ideal though as it would hold up the MyThreadObject objects unnecessarily.
Any help very gratefully appreciated!
Gareth
Comment