473,394 Members | 2,071 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,394 software developers and data experts.

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 4673
FredC <Fr***@discussions.microsoft.comwrote:
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.com>
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***@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.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.Threading.Timer,
System.Timers.Timer -- 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.DoEvents) 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.nospamwrote:

<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.com>
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.comwrote in message
news:MP************************@msnews.microsoft.c om...
Ben Voigt <rb*@nospam.nospamwrote:

<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.com>
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.nospamwrote:
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.com>
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
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...
2
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...
4
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...
6
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. ...
16
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...
7
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...
6
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. ...
2
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
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.