英文:
What happens to other threads when resource is locked in C#?
问题
我想知道当一个线程锁定了一个资源时,其他线程会做什么。
在等待锁被释放时,其他线程是否可以执行新任务(类似异步编程),还是它们什么都不做?
C# 代码示例:
lock (referenceType)
{
// 执行的代码
}
英文:
I wonder what does other threads do when one thread locked a resource.
Can other threads do new tasks while they are waiting the lock to be released (like async programming) or they just doing nothing ?
C# code example:
lock (referenceType)
{
// executing code
}
答案1
得分: 2
lock语句获取给定对象的互斥锁,执行语句块,然后释放锁。在持有锁的情况下,持有锁的线程可以再次获取和释放锁。其他线程被阻止获取锁,并等待直到锁被释放。
这个链接很有用 lock statement
英文:
The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.
This link is useful lock statement
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论