473,320 Members | 1,853 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,320 software developers and data experts.

lock statemant for loops

hi,
i have a class that contains an arraylist. The arraylist is accessed from many threads. These threads add an item into arraylist.

In order to provide thread safety, i use lock keywork. But when i do searching on the arraylist by the for loop, i dont know if i should use lock or not.

// first usage
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<myarraylist.Count;i++)
  2. {
  3.   lock(myarraylist.SyncRoot)
  4.   {
  5.     // do whatevet on arraylist
  6.   }
  7. }
// second usage
Expand|Select|Wrap|Line Numbers
  1. lock(myarraylist.SyncRoot)
  2. {
  3.   for(int i=0;i<myarraylist.Count;i++)
  4.   {
  5.      // do whatevet on arraylist
  6.   }
  7. }
Which one should i use? Or is there a more efficent way than using lock?
Aug 12 '09 #1
2 1642
Plater
7,872 Expert 4TB
I would say the 2nd example would be safer under more contexts. In the first example if another thread re-arranges or removes items from the arraylist, your loop will get all scrambled.
Depending on what you are doing in the loop and how many items there are, keeping it locked while you loop through it could slow down other threads.
Aug 12 '09 #2
Thanks Plater,

By the way, is there a better way to provide syncronizing access to collections? I use lock for now but i saw some articles about ReaderWriterLockSlim class which can seperate locking for read and write. I dont know if it is worth to change my code to use ReaderWriterLockSlim. I could not see the performance comparision between lock and ReaderWriterLock class.
Aug 12 '09 #3

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

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
0
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
1
by: Tom Dauria | last post by:
I have a form that allows the user to select some number of People from a list. The user can select one or all of the people in the list. The list has thousands of names. Once the user makes...
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
1
by: cnickl | last post by:
VS 2005 / C++ __gc program / Converted from VS2003 i have a simple statemant in my program: if(this->txt_MediaDir->Text != "") txt_MediaDir is a standart singe line textbox. For some reason...
17
by: djc | last post by:
I got great info on related question previously. This link <http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian Gideon was especially informative. 1) the lock statement does...
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
by: Curious | last post by:
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),...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.