Locking an object in C#? [closed]
I have the following sample code in some (nonexistent) language:
class Node {
int val;
Lock lock;
}
What would be the equivalent of that Lock
in C#?
In C#, any object can be used with thread locking.
So your code could look like:
class Node {
int val;
Object lockObject = new Object();
}
Within your code you lock the object like:
void SomeFunction()
{
lock(lockObject)
{
// Do sometthing that needs thread protection
}
}
If you need an inter-process locking object, then you can use a semaphore. See this.
c# has a lock keyword. You can create an object of type object called lobj or something similar and then use
object lObj = new object();
lock(lObj){}
精彩评论