[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Threads and arrays

Posted on 2006-07-11
5
Medium Priority
?
286 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 …
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…

649 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