Friday, March 17, 2006

Locking

One thing that you have to worry about in a multithreaded application is the problem of concurrency in that you do not want two seperate threads acessing a value as it is changing. .NET has a method to solve this and it is called locking.

In c# it is Lock
vb it is SyncLock

public property GetId() as integer
get
syncLock me
return _id
end syncLocl
end get
set (value as integer)
syncLock me
_id = value
end synclock
end set
end property

by calling syncLock me we lock the current object from change. This might not be what is desired so finer grained locking can be done.

locks work that only one thread can have a lock on an object.
if the object is already locked a calling thread will have to wait until the lock is released.
A single thread is allowed to acquire the same lock on an object unlimited times

0 Comments:

Post a Comment

<< Home