Solved

Threads and arrays

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 78
python question 5 69
Counting documents in a Domino View 3 66
Hide vba in gp 7 83
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

895 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

14 Experts available now in Live!

Get 1:1 Help Now