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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:40:01+00:00 2026-06-14T12:40:01+00:00

I am new to multi-threading in C#. But from reading various chapters in C#

  • 0

I am new to multi-threading in C#. But from reading various chapters in C# books and tutorials. I know that the best way to develop classes which are used in multithreaded applications is to create immutable classes.

But I am not too sure about how to deal with classes that expose a read/write property. In some articles that I have read the author has placed locks around the read/write property e.g.

public class Test
{
  private string property1;

  public string ClassProperty
  {
   get
   {
     lock 
     {
       return Property1;
     }
   }
   set
   {
      lock 
      {
        Property1 = value;
      }
   }
 }
}

I know that with any shared variable declared and used within a class that it needs to be locked in multithreaded applications. But some articles that I have read have suggested that the above code will not work if the property being exposed is a reference type. Others have suggested that you cannot make a class with exposed properties thread-safe? Does anyone have a definite answer on this topic?

Thanks

  • 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-14T12:40:03+00:00Added an answer on June 14, 2026 at 12:40 pm

    That code sample doesn’t do anything except add overhead. C# String objects by themselves are thread-safe anyway and pointer read/writes are always atomic. That code sample would only make sense for a double or a struct.

    You should not think about making individual properties thread-safe as much as making larger logical operations “atomic”. For example if you have two field foo and bar and it must always be the case that bar == lowercase(foo) then you should protect operations which access any of them with the same lock.

    A better example would be an object which must be contained in both a vector and a hash:

    • When adding you should lock both the vector and the hash and update both under the lock.
    • When querying you should lock the hash, fetch the value, unlock and return it.
    • When iterating you should either return a copy of the vector or make sure the caller understands your locking patterns.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to multi-threading and want to execute a background task using
I'm very new to Linq, I can find multi-line data reading examples everywhere (by
I'm new to iphone development. I want to develop a multi-timezone enabled alarm clock
I'm not the best with multi-threading, and this is a bit complicated, so hopefully
I'm very new to multi-threading and for some reason this class is giving me
Been looking for a multi-threading solution for my application, but keep getting random NullReferenceException
I use multi threading to copy file to another place.I from stream needed byte
Though standard doesn't guarantee the thread-safety for new , most of the multi-threading operating
I am very inexperienced using multi-threading techniques, but here is what I have tried:
I am new to mutli-threading in java and trying to understand the keyword synchronization

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.