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

Use "lock" in three methods to prevent shared-memory corruption

I have an arraylist used in three separate methods.

In method #1 (event method), some items are removed from the arraylist
if certain conditions are met;

In method #2 (event method), properties of some items are modified if
certain conditions are met;

In method #3, it loops through each item in the arraylist without
changing any item on the arraylist or removing any item from the
arraylist. In other words, this is "read-only" type of operation.

Since both #1 and #2 are event methods and may happen anytime, there's
a chance that #1, #2, and #3 all happen at the same time, then I'll
run into a memory issue when one process is modifying the arraylist,
other process(es) are accessing the arraylist.

I plan to use "lock" in all of these three methods. I have questions:

1) I know "lock" is typically used in two methods. Will there be an
issue if "lock" is used in more than two methods?

2) If I use "lock" in #1 and #2, will locking the process cause the
associated **event objects** to expire since they are dynamic in
nature and only live for a very short period of time?

I would appreciate any input!
Jul 30 '08 #1
6 1878
>1) I know "lock" is typically used in two methods. Will there be an
issue if "lock" is used in more than two methods?
Not unless you create an issue. The more threads that are locking on an
object the higher chance of a programming error which could lead to
Deadlocks. If you're careful and think about all the ways the methods are
called you should be alright.
2) If I use "lock" in #1 and #2, will locking the process cause the
associated **event objects** to expire since they are dynamic in
nature and only live for a very short period of time?
No. An event can take milliseconds or hours to process. The only reason it
would "expire" is if the developer of the object that fired the event had
some kind of timeout built in to it. From what I've seen that's not standard
practice for most apps.

--
Andrew Faust

"Curious" <fi********@yahoo.comwrote in message
news:02**********************************@k13g2000 hse.googlegroups.com...
I have an arraylist used in three separate methods.

In method #1 (event method), some items are removed from the arraylist
if certain conditions are met;

In method #2 (event method), properties of some items are modified if
certain conditions are met;

In method #3, it loops through each item in the arraylist without
changing any item on the arraylist or removing any item from the
arraylist. In other words, this is "read-only" type of operation.

Since both #1 and #2 are event methods and may happen anytime, there's
a chance that #1, #2, and #3 all happen at the same time, then I'll
run into a memory issue when one process is modifying the arraylist,
other process(es) are accessing the arraylist.

I plan to use "lock" in all of these three methods. I have questions:

1) I know "lock" is typically used in two methods. Will there be an
issue if "lock" is used in more than two methods?

2) If I use "lock" in #1 and #2, will locking the process cause the
associated **event objects** to expire since they are dynamic in
nature and only live for a very short period of time?

I would appreciate any input!
Jul 30 '08 #2
Curious wrote:
I have an arraylist used in three separate methods.

In method #1 (event method), some items are removed from the arraylist
if certain conditions are met;

In method #2 (event method), properties of some items are modified if
certain conditions are met;

In method #3, it loops through each item in the arraylist without
changing any item on the arraylist or removing any item from the
arraylist. In other words, this is "read-only" type of operation.

Since both #1 and #2 are event methods and may happen anytime, there's
a chance that #1, #2, and #3 all happen at the same time, then I'll
run into a memory issue when one process is modifying the arraylist,
other process(es) are accessing the arraylist.

I plan to use "lock" in all of these three methods. I have questions:

1) I know "lock" is typically used in two methods. Will there be an
issue if "lock" is used in more than two methods?
No. Locking ensures that only one piece of code can hold the lock at any one
time. It doesn't matter how many threads are trying that simultaneously
(though obviously performance suffers greatly if many threads are contending
for a single lock, but that's another matter altogether).
2) If I use "lock" in #1 and #2, will locking the process cause the
associated **event objects** to expire since they are dynamic in
nature and only live for a very short period of time?
What event objects? Neither event handling nor locking intrinsically create
objects.

--
J.
Jul 30 '08 #3
Are you using multiple threads? You don't need to use locking unless
you have multiple threads.

If you are using VS2005 or later you should consider using the generic
List(Of T) rather than ArrayList.

On Wed, 30 Jul 2008 09:04:16 -0700 (PDT), Curious
<fi********@yahoo.comwrote:
>I have an arraylist used in three separate methods.

In method #1 (event method), some items are removed from the arraylist
if certain conditions are met;

In method #2 (event method), properties of some items are modified if
certain conditions are met;

In method #3, it loops through each item in the arraylist without
changing any item on the arraylist or removing any item from the
arraylist. In other words, this is "read-only" type of operation.

Since both #1 and #2 are event methods and may happen anytime, there's
a chance that #1, #2, and #3 all happen at the same time, then I'll
run into a memory issue when one process is modifying the arraylist,
other process(es) are accessing the arraylist.

I plan to use "lock" in all of these three methods. I have questions:

1) I know "lock" is typically used in two methods. Will there be an
issue if "lock" is used in more than two methods?

2) If I use "lock" in #1 and #2, will locking the process cause the
associated **event objects** to expire since they are dynamic in
nature and only live for a very short period of time?

I would appreciate any input!
Jul 30 '08 #4
Hi Jack,

Thanks for asking this!

#1 and #2 are on the same main thread, while #3 is on a separate timer
thread that runs every 15 seconds.

#1 and #2 are events and may happen at anytime. What happens if #1 and
#2 happen at the same time? Are they serizlized or do they step on
each other's feet?
Jul 31 '08 #5
Hi Andrew,

Thanks for the input!

FYI, I "lock" the section of the code in all of the three methods that
use the arraylist.
Jul 31 '08 #6
Events on the same thread do not interrupt each other.

If the timer is a System.Windows.Form.Timer then it runs on the main
UI thread and will not interrupt the other events.

On Thu, 31 Jul 2008 06:17:50 -0700 (PDT), Curious
<fi********@yahoo.comwrote:
>Hi Jack,

Thanks for asking this!

#1 and #2 are on the same main thread, while #3 is on a separate timer
thread that runs every 15 seconds.

#1 and #2 are events and may happen at anytime. What happens if #1 and
#2 happen at the same time? Are they serizlized or do they step on
each other's feet?
Jul 31 '08 #7

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

Similar topics

1
by: Aaron Davies | last post by:
I'm developing a collaborative whiteboard, in which all objects (shapes, clip art icons, etc.) are synchronized between all participants in a session. It's working well, but I'm running into a...
5
by: Steven | last post by:
Can anyone tell me how to toggle the "Caps Lock" key? Thanks in advance
4
by: Scott Johnson | last post by:
Hi I am converting some code from C# to VB.NET and I have come across a command that I can't find the VB equivalent. The C# command is 'lock' and I think it is used to lock a data type from...
2
by: Ying Lu | last post by:
Hello all, I met a problem about cannot start postmaster. The situation I met is that I was running postmaster, but because of the server closed all my konsole. As a result, I did not...
1
by: Ron M. Newman | last post by:
Hi. Quick question: - I have a class with fields. There is one method that modifies them. in this method I have something like lock (this.lockObject) { /// throwing exception here... }
4
by: Joey | last post by:
Hi, I wrote a mixed-mode dll (with MFC and C++/CLI) which is called from a C#-EXE. Under special cirumstances (that is: another process sends a windows-message to my process - this message is...
2
by: titan nyquist | last post by:
I thought I need something like this, but it turns out I don't. I'm still interested if this can be done: Can you do a multi-thread "lock", that locks out everything else, all other threads,...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
6
ollyb303
by: ollyb303 | last post by:
Hello, I have created an access database to log complaint calls to my company. Sometimes these will result in a refund request being sent to our finance dept. I have an event procedure which...
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.