c# - Lock statement - does it always release the lock? -


i have read this post eric lippert regarding lock implementation in c# , still questions remain.

in 4.0 implementation if thread abort or cross thread exception occurs before monitor.exit(temp) in block executed - keep lock on object?

is there possibility exception occur @ level, leaving object still in locked state?

in 4.0 implementation if thread abort or cross thread exception occurs before monitor.exit(temp) in block executed - keep lock on object?

let's take @ code, clear other readers:

bool lockwastaken = false; var temp = obj; try  {    monitor.enter(temp, ref lockwastaken);    {      body    }  }  {    if (lockwastaken)    {     // if thread abort happens right here?     monitor.exit(temp);    } } 

your question not answerable because based on false assumption, namely, thread aborts can happen in middle of block.

thread aborts cannot happen in middle of block. 1 reason amongst many reason why should never attempt abort thread. entire thread running in block, , therefore not-abortable.

is there possibility exception occur @ level, leaving object still in locked state?

no. thread abort delayed until control leaves finally. unlocking valid lock not allocate memory or throw exception.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -