Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:24:10+00:00 2026-06-17T20:24:10+00:00

So its in C#, and basically I’ll have a position in an array for

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:24:11+00:00Added an answer on June 17, 2026 at 8:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view where the user can change some settings, its basically a
I have this Editor with me called jWYSIWYG, its basically a Jquery Plugin. So
This is contrived, but its basically what I'm doing. I have an a @helper
I have this sample in vb.net I need the same output in Delphi. its
Basically I need to reset Identity Increment for all tables to its original. Here
I have a class that is its own activity that basically i use to
I call this function at the end of my HTML file - its basically
this is extendscript for illustrator, its basically javascript though. I want the second pop
I have a table I am trying to generate reports from. Its basically a
I'm trying to turn a big list of data into a CSV. Its basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.