Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you break out of a Lock() statement?

I'm writing a program which listens to an incoming TcpClient and handles data when it arrives. The Listen() method is run on a separate thread within the component, so it needs to be threadsafe. If I break out of a do while loop while I'm within a lock() statement, will the lock be released? If not, how do I accomplish this?

Thanks!

(Any other advice on the subject of Asynchronous TCP Sockets is welcome as well.)

private void Listen()
{
    do
    {
        lock (_clientLock)
        {
            if (!_client.Connected) break;
            lock (_stateLock)
            {
                if (!_listening) break;
                if (_client.GetStream().DataAvailable) HandleData();
            }
        }
        Thread.Sleep(0);
    } while (true);
}
like image 396
dlras2 Avatar asked May 17 '10 20:05

dlras2


2 Answers

Yes. The lock statement translates into a try/finally clause. In C# 4, for example, a lock statement like so:

lock(obj)
{
    // body
}

roughly translates (taken from Eric Lippert's blog here) to:

bool lockWasTaken = false;
var temp = obj;
try 
{ 
    Monitor.Enter(temp, ref lockWasTaken); 
    { 
       // body 
    }
}
finally 
{ 
    if (lockWasTaken) 
        Monitor.Exit(temp); 
}

When the execution leaves the scope of the lock {}, the underlying lock will be released automatically. This will happen no matter how you exit scope (break/return/etc), since the call to Monitor.Exit is wrapped, internally, inside of the finally block of a try/finally.

like image 51
Reed Copsey Avatar answered Oct 16 '22 02:10

Reed Copsey


Yes, the lock will be released. You can use ILDASM or Reflector to look at the actual generated code. The lock statement is shorthand for the following code (roughly).

Monitor.Enter(_client);
try
{
  // do your stuff

}
finally {
  Monitor.Exit(_client);
}

Notice the finally block is always executed.

like image 34
Haacked Avatar answered Oct 16 '22 02:10

Haacked