Solved

Threads and arrays

Posted on 2006-07-11
5
274 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

860 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