Solved

Threads and arrays

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
for loop with Set 4 45
Exe program is not a valid Win 32 application 15 95
oracle query help 18 78
C# code editing and collaboration 3 44
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now