Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

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

Reply
 
Old 07-30-2008   #1 (permalink)
Curious


 
 

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!

My System SpecsSystem Spec
Old 07-30-2008   #2 (permalink)
Jeroen Mostert


 
 

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

Curious wrote:
Quote:

> 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).
Quote:

> 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.
My System SpecsSystem Spec
Old 07-30-2008   #3 (permalink)
Jack Jackson


 
 

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

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
<fir5tsight@xxxxxx> wrote:
Quote:

>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!
My System SpecsSystem Spec
Old 07-31-2008   #4 (permalink)
Curious


 
 

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

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?
My System SpecsSystem Spec
Old 07-31-2008   #5 (permalink)
Curious


 
 

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

Hi Andrew,

Thanks for the input!

FYI, I "lock" the section of the code in all of the three methods that
use the arraylist.
My System SpecsSystem Spec
Old 07-31-2008   #6 (permalink)
Jack Jackson


 
 

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

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
<fir5tsight@xxxxxx> wrote:
Quote:

>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?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
"there is not enough memory to complete" with shared printer Vista print fax & scan
Is it possible to define two separate "Sort" methods in the sameclass? .NET General
"there is not enough memory to complete" with shared printer Vista networking & sharing
How to prevent that the lower part of the characters <g, j, p, q, y> are "cut off" at the four top lines <From:, Date:, To:, Subject:>?? Vista mail
Prevent Expanding "User" Folder When Opening Windows Explorer Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46