473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. Public Class ParentThread
  2.     Dim _do as Dictionary(Of String, Integer)
  3.     Dim timer1 As New System.Timers.Timer
  4.  
  5.     Public Sub Start()
  6.        timer1.Enabled = True
  7.        timer1.Interval = 10
  8.        AddHandler timer1.Elapsed, AddressOf dotimer
  9.     End Sub
  10.  
  11.     Public Sub dotimer()
  12.        if staticFunctions.refreshCache() = true then
  13.           '// Some kind of SyncLock wrapper here???
  14.           _do = staticFunctions.GetNewValues()
  15.        end if
  16.        Dim workerthread As New MyThreadObject()
  17.        workerthread._doCache = _do
  18.        Dim new_worker As New Thread(AddressOf workerthread.doStuff)
  19.        new_worker.Start()
  20.     End Sub
  21. End Class
  22.  
  23. Public Class MyThreadObject
  24.     Public _doCache as Dictionary(Of String, Integer)
  25.     Public Sub doStuff()
  26.         '// Some kind of SyncLock here??
  27.         For Each kvp As KeyValuePair(Of String, String) In doCache 
  28.             '// Do stuff based on values in kvp...
  29.         Next kvp
  30.     End Sub
  31. End Class
  32.  

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
Nov 13 '09 #1
3 2275
tlhintoq
3,525 Expert 2GB
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.
Nov 13 '09 #2
Frinavale
9,735 Expert Mod 8TB
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
Nov 19 '09 #3
Infog
36
You could try
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 20 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: JennaS | last post by:
Hi. I'm new to multithreading applications in .NET. What exactly is the difference between using synclock on a variable or using mutex.waitone/mutex.releasemutex There is too much stuff out there on...
4
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first...
10
by: Bob Day | last post by:
Using vs 2003, vb.net sql msde.. Consider the following code snippets. See **** for questions. All are shared and accessed by multiple threads simultaneiously. ' Instantiate per for this...
7
by: SD | last post by:
I have a public object that I only want one thread to access at a time. However the access to this object is not limited to one procedure. Will SyncLock work in this case? If not what options do I...
1
by: fred | last post by:
I have a VB application that is using MS Access as its database. To avoid connection delays the application creates one connection to the database at start-up and maintains that single connection...
7
by: Chris Dunaway | last post by:
Suppose I have several threads that need to access the same object. ThreadA successfully acquires the lock using SyncLock. ThreadB attempts to acquire the lock and blocks and then ThreadC attempts...
1
by: Spam Catcher | last post by:
Hi all, I'm hosting a remoting service in IIS. I have a function which I only want one concurrent access at a time. Will this prevent multiple users from accessing the function at a paritcular...
2
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.