Solved

Threads and arrays

Posted on 2006-07-11
5
279 Views
Last Modified: 2010-04-17
Hi, any threading gurus out there?

I have an application that starts 6 different threads.  Each thread will create an httpwebrequest and post off to its destination and then wait for a response.

When the response returns each thread will then insert its response into an array that has been declared globally in the application.

Each thread will know its index position number in the array and so will automatically know where its response should go.

My question is this.  The part of my code which inserts into the global array could feasibly be accessed by more than one thread at a time will this cause a problem?  The answer is not obvious to me.  I would have thought that all should be OK as the index number is known and there is no danger of inserting responses in the wrong order.

However, thought I should check

Wing
0
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
neilmjohnson earned 125 total points
ID: 17079356
If each one inserts to its own array element, you should not have a problem. If they compete for a resource, then each thread would need to lock it before accessing it.  I am assuming that each thread has been assigned a unique index number when it is initiated?

BTW, the Wrox press book on Visual Basic .NET Handbook on Threading is quite good, and provides some really good concepts and information.

Neil
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17079451
It is never inherently dangerous to have the same _code_ executed by multiple threads.  That's just bread-and-butter threading.  It is also safe for threads to modify separate indices in a statically allocated array concurrently.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17079537
Array and index is OK in the case if you don't need to remove elements. If you need to remove them, I suggest you to use ArrayList or List<T>, and keep reference to added data instead of index. Container (array, list etc.) must be protected using Monitor or lock keyword, for example:

lock(this)
{
    // access data kept in container
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17079655
Additional information:
You need locking when elements are added to container and removed from it. Accessing element itself doesn't require locking, as mentioned in Alkali_Guy's post.
0
 
LVL 1

Author Comment

by:WingYip
ID: 17080123
Just needed the confirmation.  Thanks for all the help

Wing
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Introduction to Processes
Starting up a Project

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question