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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:20:30+00:00 2026-05-26T17:20:30+00:00

For being thread safe do we need to assign function parameters to the local

  • 0

For being thread safe do we need to assign function parameters to the local variables. I am going to explain it with an example

public static bool CheckEmailExist_1(string srEmail)
{
//Do some stuff with using srEmail
}

public static bool CheckEmailExist_2(string Email)
{
string srEmail=Email;
//Do some stuff with using srEmail
}

Are there any thread safe difference when these 2 functions evaluated. I mean for example lets say CheckEmailExist_1 got 100 concurrent call with of course different email parameters. Would that cause any problem during the function inside operations ?

c# 4.0 , asp.net 4.0

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-26T17:20:31+00:00Added an answer on May 26, 2026 at 5:20 pm

    Local variables won’t have issues with thread safety. Thread safety becomes a concern when there is shared state between multiple threads. In that function since you are passing the result By Value (which is the default for primitive types such as string, integers, decimals) thread safety is not an issue since there is no shared state. Objects on the other hand are passed By Reference and thread safety might becomes an issue.

    Here is a classic example. The value of _unsafe should be 10000 since you have 100 threads incrementing the _unsafe variable 100 times but it may not be when you run the program. This is because the value may be read by one thread that performs it’s calculations and then while it’s performing it’s calculations the value of the variable would be incremented by another thread. This is called a race condition and is something to avoid. Here is a great ebook on threading that covers all the topics you need to know.

    http://www.albahari.com/threading/

    public class TestThreading(){
    
        private static int _unsafe = 0;
    
        public static void main(string[] args){
            for(int i =0; i<100;i++){
                ThreadPool.QueueUserWorkItem(PerformIncrement);
            }
    
        }
    
        public static void PerformIncrement(){
            for(int i=0;i <100;i++){
                _unsafe++;
            }
        }
    }
    

    Here is another unsafe example using objects. This has the same problem as the previous example since there are multiple thread working on the same piece of data (in this case it’s the class variable “unsafe”)

    public class TestThreading2(){
    
        public int unsafe = 0;
    
        public static void main(string[] args){
            TestThreading2 objectUnsafe = new TestThreading2();
            for(int i=0;i <100;i++){
                Thread t = new Thread (PerformIncrement);
                t.Start (objectUnsafe);
            }
        }
    
        public static void PerformIncrement(object referenceParameter){
            var objectReference = (TestThreading2) referenceParameter;
            for(int i=0;i <100;i++){
                objectReference.unsafe++;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I make code like this thread safe? public static class Converter {
Why does being thread safe matter in a web app? Pylons (Python web framework)
Example: dispatch_sync(someConcurrentQueue, ^(){ dispatch_apply(5,someConcurrentQueue, ^(size_t i){ // do some non-thread safe operation }); });
I am getting a Thread was being aborted Exception in an ASP.NET page.I am
I have a Form being launched from another form on a different thread. Most
when a System.Web.HttpResponse.End() is called a System.Thread.Abort is being fired, which i'm guessing is
In .NET, after this code, what mechanism stops the Thread object from being garbage
I have a function that is being called from different threads in the application.
I have a thread-safe object which is part of an API previously used in
What is the best way to build a program that is thread safe in

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.