Multi-threaded local store slot (native storage slot) [go]

Source: Internet
Author: User
Tags net thread

1. Using the ThreadStatic feature

The threadstatic feature is the simplest use of TLS, and only supports static fields, so you can simply mark this feature on the field:

    1. [ThreadStatic]
    2. static string str = "hehe";
    3. static void Main ()
    4. {
    5. Another thread will only modify the STR variable in its own TLS
    6. Thread th = new Thread (() = {str = "MGen"; Display (); });
    7. Th. Start ();
    8. Th. Join ();
    9. Display ();
    10. }
    11. static void Display ()
    12. {
    13. Console.WriteLine ("{0} {1}", Thread.CurrentThread.ManagedThreadId, str);
    14. }

Operation Result:

1 hehe
3 MGen

As you can see, the str static fields are stored independently in two threads and are not modified by one another.

2. Using the named LocalDataStoreSlot type

Obviously the threadstatic feature only supports static fields that are too limited. The LocalDataStoreSlot in the net thread type provides better TLS support. Let's take a look at the named LocalDataStoreSlot type, which can be thread.allocatenameddataslot to allocate a named space and destroy a named space by Thread.freenameddataslot. The acquisition and setting of spatial data is through the GetData method of the thread type and the SetData method.

  1. static void Main ()
  2. {
  3. Create slots
  4. LocalDataStoreSlot slot = Thread.allocatenameddataslot ("slot");
  5. Set the value in TLS
  6. Thread.setdata (slot, "hehe");
  7. Modifying the TLS thread
  8. Thread th = new Thread (() =
  9. {
  10. Thread.setdata (slot, "MGen");
  11. Display ();
  12. });
  13. Th. Start ();
  14. Th. Join ();
  15. Display ();
  16. Clear Slots
  17. Thread.freenameddataslot ("slot");
  18. }
  19. Display the slot value in TLS
  20. static void Display ()
  21. {
  22. LocalDataStoreSlot Dataslot = Thread.getnameddataslot ("slot");
  23. Console.WriteLine ("{0} {1}", Thread.CurrentThread.ManagedThreadId, Thread.getdata (Dataslot));
  24. }

Output:

3 MGen
1 hehe

3. Using unnamed LocalDataStoreSlot types

Threads also support unnamed localdatastoreslot, unnamed localdatastoreslot do not need to be cleared manually, and the Thread.AllocateDataSlot method is required for allocation. Note Because unnamed localdatastoreslot do not have a name, Therefore, the Thread.getnameddataslot method cannot be used, only the same localdatastoreslot can be referenced in multiple threads to operate on the TLS space, and the named LocalDataStoreSlot code above is changed to an unnamed localdatast Oreslot Execution:

  1. Static LocalDataStoreSlot variables
  2. static LocalDataStoreSlot slot;
  3. static void Main ()
  4. {
  5. Create slots
  6. Slot = Thread.AllocateDataSlot ();
  7. Set the value in TLS
  8. Thread.setdata (slot, "hehe");
  9. Modifying the TLS thread
  10. Thread th = new Thread (() =
  11. {
  12. Thread.setdata (slot, "MGen");
  13. Display ();
  14. });
  15. Th. Start ();
  16. Th. Join ();
  17. Display ();
  18. }
  19. Display the slot value in TLS
  20. static void Display ()
  21. {
  22. Console.WriteLine ("{0} {1}", Thread.CurrentThread.ManagedThreadId, Thread.getdata (slot));
  23. }

4. Using the threadlocal<t> type of. NET 4.0

The. NET 4.0 thread has added a lot of things, including the threadlocal<t> type, and his appearance has made it much easier to simplify the TLS operation. Threadlocal<t> types and lazy<t> are surprisingly similar, constructor arguments are func<t> used to create objects (which can also be understood as default values for objects), and then use the Value property to get or set the object. Threadlocal's operation is somewhat more or less like the unnamed localdatastoreslot above, but Threadlocal feels more concise and better understood.

  1. static threadlocal<string> local;
  2. static void Main ()
  3. {
  4. Create threadlocal and provide default values
  5. Local = new Threadlocal<string> (() = "hehe");
  6. Modifying the TLS thread
  7. Thread th = new Thread (() =
  8. {
  9. Local. Value = "MGen";
  10. Display ();
  11. });
  12. Th. Start ();
  13. Th. Join ();
  14. Display ();
  15. }
  16. Displaying data values in TLS
  17. static void Display ()
  18. {
  19. Console.WriteLine ("{0} {1}", Thread.CurrentThread.ManagedThreadId, Local. Value);
  20. }

Output:

3 MGen

1 hehe

5. Emphasize the default values for different methods and TLS
The above code is a one-thread setting value, another thread directly modifies the value and outputs it, and does not perceive the default value in TLS, the following is a special emphasis on the default status of different methods. ThreadStatic does not provide a default value:

    1. [ThreadStatic]
    2. static int i = 123;
    3. static void Main ()
    4. {
    5. Output local thread TLS data value
    6. Console.WriteLine (i);
    7. Output another thread TLS data value
    8. ThreadPool.QueueUserWorkItem (_ = Console.WriteLine (i));
    9. Console waits for thread to end
    10. Console.readkey ();
    11. }

Output:
123

0

Obviously the local thread TLS data is 123, and the default value of the static variable is not initialized in another thread.

LocalDataStoreSlot It is easy to see that there is no default value, because initialization can only construct a space and not give it a value, Thread.setdata obviously only sets the data in TLS, or demonstrates it in code:

    1. Static LocalDataStoreSlot slot = Thread.AllocateDataSlot ();
    2. static void Main ()
    3. {
    4. Thread.setdata (slot, 123);
    5. Output local thread TLS data value
    6. Console.WriteLine (Thread.getdata (slot));
    7. Output another thread TLS data value
    8. ThreadPool.QueueUserWorkItem (_ = = Console.WriteLine (Thread.getdata (slot) = = null));
    9. Console waits for thread to end
    10. Console.readkey ();
    11. }

Output:
123
True
The second line is true, then the data in the other thread is null.

The final point:. NET 4.0 threadlocal will provide default values, remember the words I said above "threadlocal operation more or less like the above unnamed LocalDataStoreSlot"? One might ask why it created threadlocal? There's a big difference. Threadlocal can provide default values for data in TLS. (another threadlocal is a generic class, and LocalDataStoreSlot is not).

    1. Static threadlocal<int> local = new Threadlocal<int> (() = 123);
    2. static void Main ()
    3. {
    4. Output local thread TLS data value
    5. Console.WriteLine (Local. Value);
    6. Output another thread TLS data value
    7. ThreadPool.QueueUserWorkItem (_ = = Console.WriteLine (local). Value));
    8. Console waits for thread to end
    9. Console.readkey ();
    10. }

Output:
123
123

This article can also be consulted

Http://www.cnblogs.com/lulu/archive/2012/03/17/2403872.html

Multi-threaded local store slot (native storage slot) [go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.