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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:35:55+00:00 2026-05-12T17:35:55+00:00

At first I assume I do need writerlock here but Im not sure (not

  • 0

At first I assume I do need writerlock here but Im not sure (not much experience with that) what if I dont use it.

On the server side, there are client classes for each connected client. Each class contains public list which every other class can write to. Client requests are processed via threadpool workitems.

class client 
{
public List <string> A;

someEventRaisedMethod(param)
{
 client OtherClient=GetClientByID(param) //selects client class by ID sent by msg sender
 OtherCLient.A.Add("blah"); 
}

}

What if two instances reference the same client and both try OtherCLient.A.Add(“blah”)? Isnt be here some writer lock? It works for me but I encounter some strange issues that I think are due to this.

Thank you!

  • 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-12T17:35:55+00:00Added an answer on May 12, 2026 at 5:35 pm

    (update: as always, Eric Lippert has a timely blog entry)

    If you don’t use a lock, you risk either missing data, state corruption, and probably the odd Exception – but only very occasionally, so very hard to debug.

    Absolutely you need to synchronize here. I would expose a lock on the client (so we can span multiple operations):

    lock(otherClient.LockObject) {
        otherClient.A.Add("blah");
    }
    

    You could make a synchronous Add method on otherClient, but it is often useful to span multiple – perhaps to check Contains and then Add only if missing, etc.


    Just to clarify 2 points:

    • all access to the list (even reads) must also take the lock; otherwise it doesn’t work
    • the LockObject should be a readonly reference-type

    for the second, perhaps:

    private readonly object lockObject = new object();
    public object LockObject {get {return lockObject;}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please assume that I do not need to worry about development time and costs:
First, let us assume that local type inference is the sort of type inference
First hi and thanks in advance in ASP.NET : assume that i have a
First, I can assume that all urls that end with jpeg, jpg, bmp, png
I have an interesting situation, well it's probably not that unique at all, but
I need to create a recurring payment that should process only twice. First, I
Assume that I have a set of objects that need to be analyzed in
I need to style the first TD of every row that follows a column
Assume I have char **argv . First, how can I print out all the
I can only assume this is a bug. The first assert passes while the

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.