C# 代碼標準 .NET2.0版(四)多線程編碼指導方針

來源:互聯網
上載者:User

1.Use synchronization domains. Avoid manual synchronization, because that often leads to deadlocks and race conditions.

2.Never call outside your synchronization domain.

3.Manage asynchronous call completion on a callback method. Do not wait, poll, or block for completion.

4.Always name your threads:

Thread currentThread = Thread.CurrentThread;
string threadName = "Main UI Thread";
currentThread.Name = threadName;

The name is traced in the debugger Threads window, making debug sessions more productive.

5.Do not call Suspend( ) or Resume( ) on a thread.

6.Do not call Thread.Sleep( ), except in the following conditions:

 a.Thread.Sleep(0) is an acceptable optimization technique to force a context switch.

 b.Thread.Sleep( ) is acceptable in testing or simulation code.

7.Do not call THRead.SpinWait( ).

8.Do not call Thread.Abort( ) to terminate threads. Use a synchronization object instead to signal the thread to terminate.

9.Avoid explicitly setting the thread priority to control execution. You can set the thread priority based on task semantics (such as ThreadPriority.BelowNormal for a screensaver).

10.Do not read the value of the ThreadState property. Use Thread.IsAlive( ) to determine whether the thread is dead or alive.

11.Do not rely on setting the thread type to background thread for application shutdown. Use a watchdog or other monitoring entity to deterministically kill threads.

12.Do not use the thread local storage unless thread affinity is guaranteed.

13.Do not call Thread.MemoryBarrier( ).

14.Never call Thread.Join( ) without checking that you are not joining your own thread:

void WaitForThreadToDie(Thread thread)
{
   Debug.Assert(Thread.CurrentThread.ManagedThreadId != thread.ManagedThreadId);
   thread.Join( );
}

 

15.Always use the lock( ) statement rather than explicit Monitor manipulation.

16.Always encapsulate the lock( ) statement inside the object it protects:

public class MyClass

   public void DoSomething( ) 
   {
      lock(this)
      {...}
   }
}

 

17.You can use synchronized methods instead of writing the lock( ) statement yourself.

18.Avoid fragmented locking.

19.Avoid using a Monitor to wait or pulse objects. Use manual or auto-reset events instead.

20.Do not use volatile variables. Lock your object or fields instead to guarantee deterministic and thread-safe access. Do not use THRead.VolatileRead( ), Thread.VolatileWrite( ), or the volatile modifier.

21.Avoid increasing the maximum number of threads in the thread pool.

22.Never stack lock( ) statements, because that does not provide atomic locking:

MyClass obj1 = new MyClass( );
MyClass obj2 = new MyClass( );
MyClass obj3 = new MyClass( );

//Do not stack lock statements
lock(obj1)
lock(obj2)
lock(obj3)
{
   obj1.DoSomething( );
   obj2.DoSomething( );
   obj3.DoSomething( );
}

 

Use WaitHandle.WaitAll( ) instead.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.