473,765 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Locking memory and multiple timers?

I have an application that uses mulitple timers. Each of the timer event
handlers manipulate a common array of data.
I'm getting Null refererance errors - should I put a lock on the array when
I change a value?
As well, should I lock the array when I only want to read a value?
Sep 7 '06 #1
5 4696
FredC <Fr***@discussi ons.microsoft.c omwrote:
I have an application that uses mulitple timers. Each of the timer event
handlers manipulate a common array of data.
I'm getting Null refererance errors - should I put a lock on the array when
I change a value?
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

In general though, yes - you need to obtain a lock (on something, see
below) if you're going to access/change shared data.
As well, should I lock the array when I only want to read a value?
I wouldn't lock the array, but a separate locking object. See
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #2

"FredC" <Fr***@discussi ons.microsoft.c omwrote in message
news:EC******** *************** ***********@mic rosoft.com...
>I have an application that uses mulitple timers. Each of the timer event
handlers manipulate a common array of data.
Which Timer class are you using?

There's System.Windows. Forms.Timer, System.Threadin g.Timer,
System.Timers.T imer -- and they all act very different!

System.Windows. Forms.Timer will pass WM_TIMER, causing your handler to be
called on the main thread in between user actions. They will be serialized,
and locking does nothing if you're not pumping messages (think
Application.DoE vents) during a callback, and if you are -- deadlock with
non-recursive locks, and data corruption with recursive locks. But if you
prevent re-entrancy by not pumping messages during the timer processing,
everything is much simpler.

The others will create background worker threads. Locking is applicable
here.
I'm getting Null refererance errors - should I put a lock on the array
when
I change a value?
As well, should I lock the array when I only want to read a value?
If you're resizing the array, you must lock writes. Otherwise locks
shouldn't be necessary. Locking reads shouldn't be needed, although after a
resize your reads could be accessing an old copy for an extended length of
time.
Sep 7 '06 #3
Ben Voigt <rb*@nospam.nos pamwrote:

<snip>
As well, should I lock the array when I only want to read a value?

If you're resizing the array, you must lock writes.
Could you explain exactly what you mean by that?
Otherwise locks
shouldn't be necessary. Locking reads shouldn't be needed, although after a
resize your reads could be accessing an old copy for an extended length of
time.
Or they could just not "see" some of the writes at all. I tend to want
to know that the data I'm accessing is the most recent version...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #4
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Ben Voigt <rb*@nospam.nos pamwrote:

<snip>
As well, should I lock the array when I only want to read a value?

If you're resizing the array, you must lock writes.

Could you explain exactly what you mean by that?
A .NET array is ultimately pretty similar to a SAFEARRAY, it's a pointer to
a block of data and some accompanying size information. If you need to grow
the array (done automatically by ArrayList or List<T>, or explicitly by
Array.Resize()) , you end up with a pointer to an entirely different array,
and the runtime copies the data into the new block in the process. But you
could end up with still having pointers to the old block in other threads.
>
>Otherwise locks
shouldn't be necessary. Locking reads shouldn't be needed, although
after a
resize your reads could be accessing an old copy for an extended length
of
time.

Or they could just not "see" some of the writes at all. I tend to want
to know that the data I'm accessing is the most recent version...
As long as you aren't doing anything that replaces the array pointer, all
threads will be working in the same memory area. However, the optimizing
compiler could decide to cache data in a register... you need memory fences
if "apparent" order of memory access is important.
>
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 8 '06 #5
Ben Voigt <rb*@nospam.nos pamwrote:
As well, should I lock the array when I only want to read a value?

If you're resizing the array, you must lock writes.
Could you explain exactly what you mean by that?

A .NET array is ultimately pretty similar to a SAFEARRAY, it's a pointer to
a block of data and some accompanying size information. If you need to grow
the array (done automatically by ArrayList or List<T>, or explicitly by
Array.Resize()) , you end up with a pointer to an entirely different array,
and the runtime copies the data into the new block in the process. But you
could end up with still having pointers to the old block in other threads.
Absolutely - and I see what you mean about locking for writes now, in
that you don't want one thread writing to the old array when it's been
recreated.
Or they could just not "see" some of the writes at all. I tend to want
to know that the data I'm accessing is the most recent version...

As long as you aren't doing anything that replaces the array pointer, all
threads will be working in the same memory area. However, the optimizing
compiler could decide to cache data in a register... you need memory fences
if "apparent" order of memory access is important.
Exactly. That's the point I was getting at.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4562
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which has almost all of it public methods Java synchronized: > > http://cvs.sourceforge.net/viewcvs.py/jython/jython/org/python/core/PyStringMap.
2
6460
by: Sebastian Sosna | last post by:
Hello NG, iam having a problem with the code block below. All what i do is to trace processes that exited. Iam using a 5 sec timer to call the following Method: (wich i think is causing the leak) private void GetRunningProcesses(){ System.Diagnostics.Process p =
4
1090
by: Miky | last post by:
Hi, I wrote a data-entry application that has to be used by 80 people and it is using databinding to the dataset. When we were making the test on multiple machines, the application locked stating that the record that it was trying to access was locked by another machine (probably stored in the same memory page). The database is in Access. Wasn't .NET supposed to be a disconnected environment?
6
15072
by: Gene Hubert | last post by:
I seem to be getting crazy results when I have multiple System.Windows.Forms.Timer objects in the same form running at the same time. When only one timer is running I get the expected behavior. When I have two timers running at once, one fires reliably and the other fires almost never. Are there any known issues with this? Thanks, Gene H.
16
3469
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i have the following hashtable h which has multiple readers and 1 writer (on different threads) is this the correct locking below: lock (h.syncroot) {
7
2863
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
6
4093
by: shaanxxx | last post by:
I have global variable which is being shared between threads (problem is not connected with thread). Without using any mutex i have do some operation global variable in *consistent* way. consider following code : int i = 0 fun (int j)
2
1364
by: tim | last post by:
I am really new to ASP.NET and I was wondering if most developers use locking hints when accessing the data in SQL Server. What kind of multi-user issues come up when using an ASP.NET application?
15
5255
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same process. How does one maintain file locking between sibling threads? - Matt
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.