Thread Local data slot public static cscontext current
{
Get
{
Localdatastoreslot storeslot = getslot ();
Cscontext context = thread. getdata (storeslot) as cscontext;
If (context = NULL)
{
Httpcontext = httpcontext. Current;
If (httpcontext = NULL)
Throw new exception ("No cscontext exists in the current application. autocreate fails since httpcontext. Current is not accessible ");
Context = new cscontext (httpcontext );
Savecontexttostore (storeslot, context );
}
Return context;
}
}
After reading the above code, I know that the thread has a concept of a local data slot. What is the function of this item? Is it used in a single thread or multiple threads?
Related Articles:
Local thread storage (TLS)
Steps for storing local storage:
1. Apply for a data slot
Localdatastoreslot slot = thread. getnameddataslot ("para ");
If the data slot named para does not exist, a para data slot that can be used by all threads will be allocated.
2. store data in the data slot
Mypara para = new mypara ();
Para. I = I;
Thread. setdata (slot, para );
3. Release the data slot if necessary
Thread. freenameddataslot ("para ");
Be careful when releasing the data slot. This operation will cause data loss in all threads stored in the released data slot.
Steps for reading local storage:
1. Obtain specific data slots in local storage of sub-threads based on their names
Localdatastoreslot slot = thread. getnameddataslot ("para ");
2. obtain data from the data slot
Object o = thread. getdata (slot );
If (o! = NULL)
{
// Convert to a specific type
Mypara para = (mypara) O;
//.
}