This article is a mirror article of machine translation, please click here to jump to the original article.

View: 49134|Reply: 4

[Source] .NET/C# Lock Principle Monitor provides an in-depth explanation

[Copy link]
Posted on 4/29/2021 9:54:10 AM | | | |
The lock statement takes the mutex of a given object, executes a statement block, and then releases the lock. When holding a lock, the thread holding the lock can acquire and release the lock again. Any other thread is blocked from getting the lock and waits until the lock is released.

Review:

.net/c# User multithreaded concurrency lock(string){...} Detailed explanation
https://www.itsvse.com/thread-7764-1-1.html

Use the lock code

Equivalent to

It can be thought that the underlying code of Lock is implemented by Monitor.

Since the code uses try... finalblock, so even if an exception is thrown in the body of the lock statement, the lock is released.

You cannot use the await operator lock in the body of the statement.You cannot use it in the body of the statementawaitoperator lock。 You cannot use the await operator lock in the body of the statement. You cannot use the await operator lock in the body of the statement. This sentence is very important!

Documentation:The hyperlink login is visible.

I test the code myself,Use Task to get the lock and use await to block, the code is as follows:

After executing the following code several times, the execution result is not as expected.



Why? If you try to use the await keyword when trying to use the await keyword in the lock block, you will get this compilation error: cannot await in the body of a lock statement. The reason is that after await is completed, the method may run in a different thread instead of the thread (calling thread) before the await keyword.lockkeywordsame threadGet lock and release lock in the lock
Modify the code to see the reason more intuitively:

Get the thread id of the lock and the thread id of the lock release,Obviously not on a thread, so the lock cannot be successfully released, as shown in the figure below:



Tried modifying the code, tested with thread, executed multiple times, and it worked as expected, the code is as follows:



(End)





Previous:(to).NET/C# to package the folder into a zip file
Next:Redis uses Lua scripts for detailed explanations
 Landlord| Posted on 2/10/2022 9:20:09 AM |

Do not release the lock





 Landlord| Posted on 6/25/2023 7:57:43 PM |
Object synchronization method was called from an unsynchronized block of code.
The following code will give an error as above




 Landlord| Posted on 8/12/2023 7:47:49 PM |
lock syntax sugar, as shown below:






 Landlord| Posted on 7/11/2024 10:44:02 AM |
【Turn】C# multi-threaded concurrency under various locks
https://www.itsvse.com/thread-10561-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com