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

  • Home
  • SEARCH
  • 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 6845993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:34:07+00:00 2026-05-27T00:34:07+00:00

I am just curious if it possible to use const value as lock, for

  • 0

I am just curious if it possible to use const value as lock, for example:

private const string counterKey = "Requests_Sec";
public static object RequestsPerSecond()
{
   lock (counterKey)
   {
       // do something...
   }
}

I presumed const is static so I used it in this situation; and am curious if reverting to static string is what solved the problem.

EDIT: Most of you are saying that string is the problem rather than const. To rephrase my question – would this be OK for locking:

private const object counterKey = new object();

EDIT: My bad – you can’t have const object… what I take away from this is always lock on object _sync = new object(); and make it static if needed.

  • 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-27T00:34:08+00:00Added an answer on May 27, 2026 at 12:34 am

    This about locking on string rather then locking on a static const. There are only 2 types of variables that can be marked const and those are primitive value types and strings. On the first one you can’t lock. Note that due to string intering, making the string const or not wont likely make much difference in regards to the instance of the string.

    For an answer on the question how locking on strings work, see: Using string as a lock to do thread synchronization

    Basically, it’s not safe to lock on a string.

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

Sidebar

Related Questions

Possible Duplicate: Why should I use templating system in PHP? I was just curious
Just curious here: is it possible to invoke a Windows Blue Screen of Death
Just curious how you would comment this line of code: string customerNm = customerNm.EndsWith(s)
Just a curious question. Is it possible to get the editor to allow the
Given an example such as .. public interface IInterface { } public static void
Is it possible to redefine which object the brackets [] use? I can subclass
I am newbie for template engines Just I am curious is it possible to
Possible Duplicate: Using D programming language in a .NET context Just curious, is it
Just curious: 4 instanceof Number => false new Number(4) instanceof Number => true? Why
Just curious what is the best practice for solving the following issue. I have

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.