[英]How to make my count thread-safe?
I have a controller and its sole function is to increase a count in a model (ie foo_count
) and load a view. 我有一个控制器,其唯一功能是增加模型中的计数(即
foo_count
)并加载视图。
Lets assume I have 2 web instances running. 假设我有2个Web实例正在运行。 If 10 concurrent users were to hit this page/controller at the same time.
如果有10个并发用户同时访问此页面/控制器。 Will my count be 10?
我的数目是10吗?
Will there be a race condition of some sort? 会有某种比赛条件吗? Since they are concurrent hits, both web requests will load a copy of model
Foobar
, with foo_count
equals to 0 via FoobarController
. 由于他们是同时命中,两个web请求将加载模型的副本
Foobar
,与foo_count
通过等于0 FoobarController
。
This means they were both operating on their own copy of Foobar
that wasn't aware of the change the other web instance was doing. 这意味着他们俩都在自己的
Foobar
副本上进行操作,而Foobar
却不知道另一个Web实例正在做的更改。 Which also means, the count will unlikely be 10. 这也意味着,计数不可能是10。
What are some ways to resolve this? 有哪些解决方法?
您应该使用内置记录锁定来避免出现竞争情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.