. The analysis of misuse point in net multithreading programming

Source: Internet
Author: User
This paper mainly introduces the. NET multithreaded programming in the misuse point analysis. Have a certain reference value, follow the small series below to see it

1 Shared variable issues

Error wording:

All tasks may share the same variable, so the output may be the same.

public static void Error () {   for (int i=0;i<10;i++)   {    Task.run () = {Console.WriteLine ("{0}", i);});   }}

Correct wording:

Assign the variable i to the local variable temp so that each task uses a different I value.

public static void Right () {for   (int i = 0; i <; i++)   {    int temp = i;    Task.run (() = {Console.WriteLine ("{0}", temp),});}   }

2 Do not clean up the resources required to suspend a task

Error wording:

Asynchronously outputs the text content, so when the StreamReader is not used, the variable SR has left its scope, calling the Dispose method.

public static void Error () {   using (StreamReader sr = new StreamReader (@ "d:\ description. txt", encoding.default))   {    Task.run (() = {Console.WriteLine ("Output: {0}", Sr. ReadLine ()); });   }}

Correct wording:

public static void Right () {   using (StreamReader sr = new StreamReader (@ "d:\ description. txt", encoding.default))   {    var task = Task.run (() = {Console.WriteLine ("Output: {0}", Sr. ReadLine ()); });    Task. Wait ();   }}

3 Avoid locking this,typeof (type), string

The right thing to do: Define a private read-only field of type object, and lock it.

4 the number of waithandles on WaitHandle.WaitAll must be less than or equal to 64

public static void Error () {   manualresetevent[] manualevents = new manualresetevent[65];   Try   {for    (int i = 0; i < i++)    {     var temp = i;     Task.run (()     = {      Manualevents[temp] = new ManualResetEvent (false);      Console.WriteLine ("{0}", temp);      Manualevents[temp]. Set ();     });    }    WaitHandle.WaitAll (manualevents);   }   catch (Exception ae)   {    Console.WriteLine (AE. Message);}   }

5 scenarios in which an exception cannot be caught

try{    var task = Task.run (() = {throw new Exception ("throw exception");});    If you drop this line of code, you cannot throw an exception    task. Wait ();} catch (Exception ex) {    Console.WriteLine (ex. Message);}

6 whether to release the task resource

It is recommended to call Dispose, but not to call nor a serious error.

Note that a task task is not allowed to release resources when it is in some state, otherwise it will error.

public static void Catchexception () {   try   {    Console.WriteLine ("Start");    var task = Task.run (() =    {     //throw new Exception ("throw exception");     });    Note the following line of code and observe the exception result    //task. Wait ();    Task. Dispose ();    Console.WriteLine ("End");   }   catch (Exception ex)   {    Console.WriteLine (ex. Message);}   }

7 Deadlock Demo

It is assumed that both TSAK1 and Task2 acquire a second lock (for TSAK1, the second lock it requests is LockedObj2, and Task2 for LockedObj1) A deadlock occurs before the first lock is successfully obtained.

private static readonly Object LockedObj1 = new Object ();p rivate static readonly Object LockedObj2 = new Object ();p ublic s tatic void Lockshow () {   var task1 = Task.run (() + =    {    lock (LockedObj1)    {     Console.WriteLine ("Get LockedObj1 ");     Lock (LockedObj2)     {      Console.WriteLine ("Get LockedObj2 ....");}}   );   var task2 = Task.run (() =   {{    lock (LockedObj2)    {     Console.WriteLine ("Get LockedObj2");     Lock (LockedObj1)     {      Console.WriteLine ("Get LockedObj1 ....");}}   );

The following two results are available for multiple runs: The first figure is the case where there is no deadlock, and the second diagram is the case of a deadlock.

8 Do not call the Thread.Abort method.

The task does not provide an abort method, and with the new TPL (. NET 4.0), you do not think of this problem and generally use CancellationToken to control cancellation.

9 Ensure that shared variables are safe

Running repeatedly, you can observe a different result, as shown in.

public static void Func () {   string s = "ASDFGH";   Parallel.Invoke (    () = {s = s.replace ("A", "1"); s = s.replace ("s", "1s");},     () = {s = s.replace ("A", "2 "); s = S.replace ("s", "2s"); },     () = {s = s.replace ("A", "3");});   Console.WriteLine (s);}

10 processor excess application and insufficient application

public static void Func () {   paralleloptions po = new ParallelOptions ();   Over-application, the processor has only 4 logical cores, the results set the degree of parallelism of 10 and is a logical core is working, the number of waiting tasks is greater than 0.   Po. Maxdegreeofparallelism = ten;   Insufficient application, the processor has 4 logical cores, but specifies a degree of parallelism of 3, there is an idle kernel is not occupied (and may be occupied by other threads, this assumes that in the case of a specified degree of parallelism of 3, another core idle)   PO. Maxdegreeofparallelism = 3;   list<int> list = new list<int> ();   Parallel.ForEach (list, po, m + =   {    //Business   });}
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.