multithreading - Use of lock keywork c# -
i working on c# coding exercise code:
class program { static object sync = new object(); static void saferun() { lock (sync) { thread.sleep(1000); } } static void main(string[] args) { lock (sync) { saferun(); } console.write("safe"); } } }
what printed?
- nothing, deadlock occurs.
- it not compile.
- "safe" print.
i thought deadlock occur, when run code "safe" printed.
so, can explain me why 3 correct , why 1 not correct?
thank you!
for deadlock
occur need @ least 2 threads want access resources locked between them.
example:
you have 2 running thread
, 2 list<t>
.
thread a
locked list a
. thread a
want take value list b
thread b
locked list b
. thread b
want take value list a
now both threads
try resources locked between them.
Comments
Post a Comment