Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple threads to write to Array

So its in C#, and basically I'll have a position in an array for each thread to store some data. Would I Need to lock this? For example:

int[] threads = new int[12];

Each thread would access a specific position in the array for example, thread 1 will update the value in threads[0], thread 2 threads[1], etc.

Idea is to have the console print the values that are stored in the array.

Alright got so many comments. I thought I would clarify exactly what I am doing in the hopes I will learn even more. So basically the gist of it is:

The main thread kicks off 12 separate threads, each thread calls a function in the main thread to get a bunch of records from the database. The access to that method is locked but it returns about a 100 records for the thread to process by itself.

As the thread is processing the records it makes a couple of web requests and inserts into the database. Once the thread has finished processing its batch of records it calls a function again from the main thread and that function kicks off a new thread in lieu of the last one being finished.

As the threads are doing their processing I would like to output their progress in the console. Initially I locked each console output because if the same function gets called simultaneously the cursor position for each output would go all over the place. So I was thinking I would have an array that stored the counts for each value and then have a function just print it all out. Although I am starting to wonder if that would really be any different than what I currently am doing.

like image 880
sridawg Avatar asked Jan 20 '13 01:01

sridawg


People also ask

Can multiple threads write to the same array?

The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array. Modifications of different elements therefore do not write to any of the same memory locations.

Can you make an array of threads?

You can create the array of threads (or list of threads, even) anywhere outside the for loop. Yes, collect them in an array.

What happens when 2 threads try to modify an ArrayList?

There is no guaranteed behavior for what happens when add is called concurrently by two threads on ArrayList. However, it has been my experience that both objects have been added fine. Most of the thread safety issues related to lists deal with iteration while adding/removing.

Can multiple threads run concurrently?

Concurrency and Parallelism In the same multithreaded process in a shared-memory multiprocessor environment, each thread in the process can run concurrently on a separate processor, resulting in parallel execution, which is true simultaneous execution.


2 Answers

If each thread is accessing a value at its own index, then you should be fine as you don't have to worry about access from multiple threads simultaneously.

like image 102
Justin Niessner Avatar answered Oct 07 '22 02:10

Justin Niessner


I believe that if each thread only works on a separate part of the array, all will be well. If you're going to share data (i. e. communicate it between threads) then you'll need some sort of memory barrier to avoid memory model issues.

I believe that if you spawn a bunch of threads, each of which populates its own section of the array, then wait for all of those threads to finish using Thread.Join, that that will do enough in terms of barriers for you to be safe.

MSDN documentation on Arrays says:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for an Array; however, .NET Framework classes based on Array provide their own synchronized version of the collection using the SyncRoot property.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

So no, they're not thread safe. Generally a collection is said to be 'not threadsafe' when concurrent accesses could fail internally, but as each thread will access different possitions, there is no concurrent access here.

like image 35
canolucas Avatar answered Oct 07 '22 02:10

canolucas