SyncLock

  • Thread starter Thread starter Elisa
  • Start date Start date
E

Elisa

Hi,

I'm still confused about the parameter you use for SyncLock (or
Monitor.Enter) calls.

If I want to create the equivalent of critical sections, I should be
able to write something like the following code extract, not?

Private MyLock As New Object
....
SyncLock MyLock
...
End SyncLock

As long as MyLock can be seen by all the threads, each thread should
block when arriving at the SyncLock statement, correct?


Regards,

Elisa
 
Elisa said:
I'm still confused about the parameter you use for SyncLock (or
Monitor.Enter) calls.

You use whatever object you want. Some people use Me, but I prefer to
have an object which is *just* used for locking, and only for locking
specific bits of data (which should appear in the comment for it).
If I want to create the equivalent of critical sections, I should be
able to write something like the following code extract, not?

Private MyLock As New Object
...
SyncLock MyLock
...
End SyncLock

As long as MyLock can be seen by all the threads, each thread should
block when arriving at the SyncLock statement, correct?

Apart from the first one, of course - yes, that's right.
 
Hi Elisa,

That is correct, any object you use will be the source of a MUTEX lock.
Therefore, it does not really matter as long as everyone syncing to it can
see it, as you stated. I typically use this/Me but sometimes, for example
when doing audio, I only lock on the specific object that will be accessed
by the threaded procedure. That way, the code can still be run
simultaneously on two separate objects.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top