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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:24:59+00:00 2026-05-28T08:24:59+00:00

I am trying to investigate locking to create a threadsafe class and have a

  • 0

I am trying to investigate locking to create a threadsafe class and have a couple of questions. Given the following class:

    public class StringMe 
    {
        protected ArrayList  _stringArrayList = new ArrayList();
        static readonly object _locker = new object();

        public void AddString(string stringToAdd)
        {
            lock (_locker) _stringArrayList.Add(stringToAdd);
        }

        public override string ToString()
        {
            lock (_locker)
            {
return string.Join(",",string[])_stringArrayList.ToArray(Type.GetType("System.String")));
            }
        }
    }

1) Did I successfully make AddString andToString threadsafe?

2) In the ToString method I’ve created is it necessary to lock there to make it threadsafe?

3) Is it only the methods that modify data that need to be locked or do both the read and write opperations need to be locked to make it threadsafe?

Thank you so much for your time!

  • 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-28T08:25:00+00:00Added an answer on May 28, 2026 at 8:25 am

    No, you haven’t made those calls thread-safe – because the _stringArrayList field is protected. Subclasses could be doing whatever they like with it while AddString and ToString are being called.

    For example (as the other answers claim that your code is thread-safe.)

    public class BadStringMe : StringMe
    {
        public void FurtleWithList()
        {
            while (true)
            {
                _stringArrayList.Add("Eek!");
                _stringArrayList.Clear();
            }
        }
    }
    

    Then:

    BadStringMe bad = new BadStringMe();
    new Thread(bad.FurtleWithList).Start();
    bad.AddString("This isn't thread-safe");
    

    Prefer private fields – it makes it easier to reason about your code.

    Additionally:

    • Prefer List<T> to ArrayList these days
    • You’re locking with a static variable for some reason… so even if you’ve got several instances of StringMe, only one thread can be in AddString at a time in total
    • Using typeof(string) is much cleaner than Type.GetType("System.String")

    3) Is it only the methods that modify data that need to be locked or do both the read and write opperations need to be locked to make it threadsafe?

    All, assuming that there might be some operations. If everything is just reading, you don’t need any locks – but otherwise your reading threads could read two bits of data from the data structure which have been modified in between, even if there’s only one writing thread. (There are also memory model considerations to bear in mind.)

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

Sidebar

Related Questions

Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I am trying to understand JavaScript minification and compression processes and have couple of
I'm trying to investigate sphinx rt indexes to use in future project and have
I am trying to investigate how to develop an UI application that will run
I'm trying to investigate the state of the C/C++ heap from within gdb on
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm working with an iPhone 3G, and when I'm trying to investigate memory leaks
I'm trying to investigate why would my app, written in VC 2008, crash on
I have the following PHP code which simply grabs a URL behind HTTP-Basic authentication
I'm attempting to use Wix to create a database during install. I have my

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.