什么是C#中的无锁会话?
我了解C#中的会话以及如何定义它们.但是今天我听到了一个术语无锁会话".我用谷歌搜索,但没有得到与我的问题完全匹配的答案.谁能解释一下C#中的无锁会话,以及如何为它们编写代码?
I know about the sessions in C# and how to define them as well. But today I heard a term Lock-free sessions. I googled it but did not get any answer exactly matching to my question. Can anyone please explain something about lock-free sessions in C# and how to write a code for them?
以下是
对ASP.NET会话状态的访问是每个会话的独占,这意味着如果两个不同的用户发出并发请求,则将同时授予对每个单独会话的访问权限.但是,如果对同一会话发出了两个并发请求(通过使用相同的SessionID值),则第一个请求将获得对会话信息的互斥访问.仅在第一个请求完成后才执行第二个请求.(如果由于第一个请求超过了锁定超时而释放了对该信息的排他锁,则第二个会话也可以访问.)如果@ Page指令中的EnableSessionState值设置为ReadOnly,则该请求为只读会话信息不会导致会话数据互斥锁定.但是,对会话数据的只读请求可能仍必须等待读写数据会话请求的锁设置才能清除. Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear. 因此,每当并发请求带有相同的sessionId时,它都将进入互斥锁.要创建无锁会话,您只需要按照MSDN上的上述文档,将 so whenever the concurrent request comes with the same sessionId it just enters in exclusive lock. To create lock free session you just need to set 注意:当您将EnableSessionState指定为ReadOnly时.asp.net不会在会话上获得任何排他锁,最终还会使该页面的会话变为只读. 关于在另一个Stack溢出线程上的asp.net中的会话锁定的讨论非常好:- Here is very good discussion about the session locks in asp.net on another Stack overflow thread:- link
EnableSessionState
设置为 ReadOnly
.这称为无锁会话.EnableSessionState
to ReadOnly
as per above documentation from MSDN. And this is called lock-free session.