简体   繁体   English

C#中的无锁会话是什么?

[英]What are the lock-free sessions in C#?

I know about the sessions in C# and how to define them as well. 我知道C#中的会话以及如何定义它们。 But today I heard a term Lock-free sessions. 但是今天我听到了无锁会话这个词。 I googled it but did not get any answer exactly matching to my question. 我用Google搜索,但没有得到任何与我的问题完全匹配的答案。 Can anyone please explain something about lock-free sessions in C# and how to write a code for them? 任何人都可以解释一下C#中的无锁会话以及如何为它们编写代码吗?

Here is some content from msdn section Concurrent Request and Session State 以下是msdn部分Concurrent Request and Session State一些内容

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. 但是,如果对同一会话发出两个并发请求(通过使用相同的SessionID值),则第一个请求将获得对会话信息的独占访问权。 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. (如果由于第一个请求超过锁定超时而释放信息的独占锁定,则第二个会话也可以访问。)如果@ Page指令中的EnableSessionState值设置为ReadOnly,则只读请求会话信息不会导致会话数据的独占锁定。 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. 但是,会话数据的只读请求可能仍然必须等待由会话数据的读写请求设置的锁定才能清除。

so whenever the concurrent request comes with the same sessionId it just enters in exclusive lock. 因此,只要并发请求带有相同的sessionId,它就会进入独占锁。 To create lock free session you just need to set EnableSessionState to ReadOnly as per above documentation from MSDN. 要创建无锁会话,您只需按照上述MSDN文档将EnableSessionState设置为ReadOnly And this is called lock-free session . 这称为无锁会话

Note: when you specify EnableSessionState as ReadOnly. 注意:将EnableSessionState指定为ReadOnly时。 asp.net would not acquire any exclusive lock on session and eventually it also makes session as readonly for that page. asp.net不会获得会话的任何独占锁定,并最终它也会使会话成为该页面的只读。

Here is very good discussion about the session locks in asp.net on another Stack overflow thread:- link 以下是另一个Stack溢出线程中关于asp.net中会话锁的非常好的讨论: - 链接

The session state module implements a readers – writers locking mechanism and queues the access to session state values. 会话状态模块实现readers – writers locking mechanism写入readers – writers locking mechanism ,并对对会话状态值的访问进行排队。 A page that has session-state write access will hold a writer lock on the session until the request finishes. 具有会话状态写访问权限的页面将在会话上保持写入程序锁定,直到请求完成。 A page gains write access to the session state by setting the EnableSessionState attribute on the @Page directive to True. 通过将@Page指令上的EnableSessionState属性设置为True,页面获得对会话状态的写访问权。 A page that has session-state read access — for example, when the EnableSessionState attribute is set to ReadOnly — will hold a reader lock on the session until the request finishes. 具有会话状态读取访问权限的页面(例如,当EnableSessionState属性设置为ReadOnly )将在会话中保持读取器锁定,直到请求完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM