PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Thread synchronization.

Reply

Thread synchronization.

 
Thread Tools Rate Thread
Old 16-11-2006, 01:58 AM   #1
Blake
Guest
 
Posts: n/a
Default Thread synchronization.


Heya Group.

I am hoping that someone can help me understand how to use Synclock
properly. I am a tad confused. I have a class that is instanced on
multple threadpool threads, and uses shared members to track the state
shared by all instances.

The web is littered with examples of people using ...

Synclock GetType(MyClass)
' Do something to shared class members.
End Synclock

.... to access static members when many threads may own instances of the
class.

I understand that is dangerous because the type object may be locked
from outside the object causing deadlocks, and I am led to believe that
a private locking object is the way to go.

So should I do something like this;

Private Shared _lockObject As Object
....
Synclock _lockObject
' Do something to shared class members.
End Synclock

Or should I be locking on a type rather than an instance?

Thanks.

  Reply With Quote
Old 16-11-2006, 06:09 AM   #2
Tom Shelton
Guest
 
Posts: n/a
Default Re: Thread synchronization.


Blake wrote:
> Heya Group.
>
> I am hoping that someone can help me understand how to use Synclock
> properly. I am a tad confused. I have a class that is instanced on
> multple threadpool threads, and uses shared members to track the state
> shared by all instances.
>
> The web is littered with examples of people using ...
>
> Synclock GetType(MyClass)
> ' Do something to shared class members.
> End Synclock
>
> ... to access static members when many threads may own instances of the
> class.
>
> I understand that is dangerous because the type object may be locked
> from outside the object causing deadlocks, and I am led to believe that
> a private locking object is the way to go.
>
> So should I do something like this;
>


One correction, make sure initialize the instance

Private Shared _lockObject As New Object
^^^^
> ...
> Synclock _lockObject
> ' Do something to shared class members.
> End Synclock
>
> Or should I be locking on a type rather than an instance?
>
> Thanks.


Look on a private shared object that you create (your second exampl) -
never lock on the type. That is a result of some bad MSDN examples...

--
Tom Shelton

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off