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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:09:00+00:00 2026-05-12T12:09:00+00:00

Okay, I just can’t get my head around multi-threading scenarios properly. Sorry for asking

  • 0

Okay, I just can’t get my head around multi-threading scenarios properly. Sorry for asking a similar question again, I’m just seeing many different “facts” around the internet.

public static class MyClass {
    private static List<string> _myList = new List<string>;
    private static bool _record;

    public static void StartRecording()
    {
        _myList.Clear();
        _record = true;
    }

    public static IEnumerable<string> StopRecording()
    {
        _record = false;
        // Return a Read-Only copy of the list data
        var result = new List<string>(_myList).AsReadOnly();
        _myList.Clear();
        return result;
    }

    public static void DoSomething()
    {
        if(_record) _myList.Add("Test");
        // More, but unrelated actions
    }
}

The idea is that if Recording is activated, calls to DoSomething() get recorded in an internal List, and returned when StopRecording() is called.

My specification is this:

  • StartRecording is not considered Thread-Safe. The user should call this while no other Thread is calling DoSomething(). But if it somehow could be, that would be great.
  • StopRecording is also not officially thread-safe. Again, it would be great if it could be, but that is not a requirement.
  • DoSomething has to be thread-safe

The usual way seems to be:

    public static void DoSomething()
    {
        object _lock = new object();
        lock(_lock){
            if(_record) _myList.Add("Test");
        }
        // More, but unrelated actions
    }

Alternatively, declaring a static variable:

    private static object _lock;

    public static void DoSomething()
    {
        lock(_lock){
            if(_record) _myList.Add("Test");
        }
        // More, but unrelated actions
    }

However, this answer says that this does not prevent other code from accessing it.

So I wonder

  • How would I properly lock a list?
  • Should I create the lock object in my function or as a static class variable?
  • Can I wrap the functionality of Start and StopRecording in a lock-block as well?
  • StopRecording() does two things: Set a boolean variable to false (to prevent DoSomething() from adding more stuff) and then copying the list to return a copy of the data to the caller). I assume that _record = false; is atomic and will be in effect immediately? So normally I wouldn’t have to worry about Multi-Threading here at all, unless some other Thread calls StartRecording() again?

At the end of the day, I am looking for a way to express “Okay, this list is mine now, all other threads have to wait until I am done with it”.

  • 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-12T12:09:00+00:00Added an answer on May 12, 2026 at 12:09 pm

    I will lock on the _myList itself here since it is private, but using a separate variable is more common. To improve on a few points:

    public static class MyClass 
    {
        private static List<string> _myList = new List<string>;
        private static bool _record; 
    
        public static void StartRecording()
        {
            lock(_myList)   // lock on the list
            {
               _myList.Clear();
               _record = true;
            }
        }
    
        public static IEnumerable<string> StopRecording()
        {
            lock(_myList)
            {
              _record = false;
              // Return a Read-Only copy of the list data
              var result = new List<string>(_myList).AsReadOnly();
              _myList.Clear();
              return result;
            }
        }
    
        public static void DoSomething()
        {
            lock(_myList)
            {
              if(_record) _myList.Add("Test");
            }
            // More, but unrelated actions
        }
    }
    

    Note that this code uses lock(_myList) to synchronize access to both _myList and _record. And you need to sync all actions on those two.

    And to agree with the other answers here, lock(_myList) does nothing to _myList, it just uses _myList as a token (presumably as key in a HashSet). All methods must play fair by asking permission using the same token. A method on another thread can still use _myList without locking first, but with unpredictable results.

    We can use any token so we often create one specially:

    private static object _listLock = new object();
    

    And then use lock(_listLock) instead of lock(_myList) everywhere.

    This technique would have been advisable if myList had been public, and it would have been absolutely necessary if you had re-created myList instead of calling Clear().

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

Sidebar

Related Questions

Okay, this should be simple, but I just can't get it to work. I
Okay so I've been set an assignment from university and I just can't get
Okay I just typed this whole question out and then managed to delete it.
okay, I guess I'm tired because I just can't find the syntax error with
okay so this seemed like it should be pretty basic but I just can't
Okay, after long searching, hesitating and more searching I just can't seem to figure
Okay, just learnt LINQ syntax about ten minutes ago, made a first program: public
I just want to ask if is it okay that I use native PHP
Okay, I think I'm just making a stupid mistake here, but I want to
Is okay to break all dependencies using interfaces just to make a class testable?

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.