Synclock clarification

J

Johan Karlsson

Hi all!

I just need a true or false answer for this statement.
Consider having a piece of code that boils down to this

Dim a = new ArrayList
SyncLock a
SyncLock a
a.Add("Something")
End SyncLock
End SyncLock

The question: Since it is the same thread that locks the resource twice the
add function will execute without problems, right? And other threads trying
to lock will just have to wait 'til their turn?

Another question while im at it: If all lockings withing an assembly only
refer to a single object, that is there is not "nested" locking, a deadlock
can never occour? Right or wrong? Need some more clarification? :)

Thanks!

/Johan
 
D

drew

True and yes there can be deadlocks still if you have more than one thread
and more than one lockable resource.
 
J

Johan Karlsson

Hi and thank you for the reply!


On the second question,

Could you explain to me how that deadlock would occure. That is, there is no
code that will lock two object within the same thread. Example, thread tA
will never lock object oA and oB at the same time, it will first lock oA, do
its business and then lock (and release oA) object oB. Thread tB will have
the same "rules" and never lock oA and oB at the same time.

I just cant figure out how a deadlock can occure then? But I'm kindofa
newbie in the threadworld.

Thanks

/Johan
 
D

drew

You're correct in your assumption; as long as you code carefully and make
sure that you always release the first object before trying to acquire the
second object you should never have any deadlocks. You'll also want to make
sure you have finally's around to release the locks as well... with
threading always land on the carefull side.

drew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top