其他线程在C#中锁定资源时会发生什么?

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月23日 21:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定