Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have a following class at server-side.

public class Sample
{
    private enum Status
    {
        NotEvaluated,
        Yes,
        No
    }
    private static object _lockObj = new object();
    private static Status _status = Status.NotEvaluated;
    public static Status GetStatus()
    {
        if (_status == Status.NotEvaluated)
        {
            lock (_lockObj)
            {
                if (_status == Status.NotEvaluated)
                {
                    //some evaluation code which sets status to either Yes/No;
                    _status = Status.Yes;
                }
            }
        }
        return _status;
    }
}


Is anything wrong in locking mechanism above? do i need to lock at all? Because it is server-side (multiple requests will be there) and the variable is static i would think it should be locked at the time of evaluation.

correct me if i am wrong.

Thanks
Posted

1 solution

The lock statement only makes sense if anything in your protected block is accessed by some other thread of the same process. If this is the case — the code is probably correct, if not — it is redundant at best.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900