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 7071817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:44:01+00:00 2026-05-28T05:44:01+00:00

Im just confused why adding to a list would not be thread safe like

  • 0

Im just confused why adding to a list would not be thread safe like below

object aLock = new object();
List<string> aList = new List<string>

lock(aLock)
   aList.Add("abc");

Not sure why a lock would be required where all you are doing is adding to it.
Why would such a scenario be not thread safe?

  • 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-05-28T05:44:02+00:00Added an answer on May 28, 2026 at 5:44 am

    The example code is useless- as @Jon mentions, all threads would be locking on their own object, which means they wouldn’t be blocking each other at all. Might as well leave out the lock statement entirely.

    First of all, you need to lock on an object that’s common to all of the threads (like the list itself). For instance:

    lock (aList)
        aList.Add("abc");
    

    As for “why”, the internal implementation of the List may (does) perform actions that are not safe to perform in parallel on multiple threads. This is documented in the List class MSDN docs:

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

    A List can support multiple readers concurrently, as long as the
    collection is not modified. Enumerating through a collection is
    intrinsically not a thread-safe procedure. In the rare case where an
    enumeration contends with one or more write accesses, the only way to
    ensure thread safety is to lock the collection during the entire
    enumeration. To allow the collection to be accessed by multiple
    threads for reading and writing, you must implement your own
    synchronization.

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

Sidebar

Related Questions

I'm very VERY new at this whole web thing. And I'm just very confused
Help, I'm confused. I just need to get a JSON object into my page.
Just a little confused here... I have a function in postgres, and when I'm
I am bit confused about ADO.Net Data Services. Is it just meant for creating
I'm just starting to try to pick up the J language, and am confused
I've just recently learned the PureMVC framework, and am a little confused as to
I have just, in my groggy morning state, reversed & confused the arguments to
I have just moved to xcode4 and boy am i confused! :) For one,
I would like to know if anyone can help me out with a problem
I just started a new job yesterday and this is only my second job

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.