开发者

Locking an object in C#? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and canno开发者_Python百科t be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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){}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜