对静态变量的任何锁定机制? [英] Any lock machanism to static variables ?
本文介绍了对静态变量的任何锁定机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在锁定静态变量的机制,而其他用户访问它以克服concurreny问题。
例如: - User1访问静态变量,User2& User3应该等到User1释放它。
请帮助。
我尝试过: < br $>
尝试了不同的选项但是需要静态变量但是在并发用户时面临问题。
Is there any machanism to lock static variable while other user accessing it to overcome concurreny issue.
Ex:- User1 accessing static variable, User2 & User3 should wait until it is released by User1.
Please help.
What I have tried:
Tried different options but static variable is required but facing issue while concurrent users.
推荐答案
通常,使用静态变量是一个很好的信号,表明你的设计存在缺陷,但偶尔也需要它们。
最简单的方法是使用lock语句(C#参考)| Microsoft Docs [ ^ ] - 但请注意,您将对任何致命的拥抱或类似问题负责。
我仔细看看您的代码/设计以及您为什么使用静态变量来代替我自己使用静态变量。
Normally, using a static variable is a good sign that your design is flawed, but they are very occasionally necessary.
The simplest way to do it is to use a lock Statement (C# Reference) | Microsoft Docs[^] - but do be aware that you will be responsible for any deadly embrace or similar problems.
I'd have a close look at your code / design and why exactly you are using static variables for multiuser access instead myself.
您可以使用 Mutex 类。
检查此示例: https://www.c-sharpcorner.com/UploadFile/1d42da/threading-with-mutex/
You can use Mutex class.
Check this example : https://www.c-sharpcorner.com/UploadFile/1d42da/threading-with-mutex/
这篇关于对静态变量的任何锁定机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文