Brief SyncLock / Multithreading question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clanjenkins
    New Member
    • Nov 2009
    • 1

    Brief SyncLock / Multithreading question

    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:

    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
    Last edited by tlhintoq; Nov 13 '09, 03:46 AM. Reason: [CODE] ...your code here... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      It's always very difficult to figure out how to handle resources that are shared between threads.

      I could be wrong here because I've only rarely ever used threads..

      I would think that the only time that you need to SyncLock the shared resource is when you are modifying it.

      From what I understand about the SyncLock Statement is that no threads will be able to access the resource when they hit the SyncLock statement.


      -Frinny

      Comment

      • Infog
        New Member
        • Dec 2008
        • 36

        #4
        You could try
        Code:
        Threading.ReaderWriterLockSlim
        instead. This allows you to specify if a piece of code can read, read then write, or just write.

        Any thread can read at the same time (no writing), only one thread can have the privilege of reading then upgrade into writing, and only one thread can write (no reading).

        There is a good explanation here: http://www.albahari.com/threading/

        Hope this helps.

        Comment

        Working...