IDesign C#編碼規(guī)范(之八)
2024-07-21 02:18:48
供稿:網(wǎng)友
4.4多線程 multithreading
1.應用同步機制空間。
use synchronization domains. see chapter 8 in programming .net components.
避免手工的同步機制,應為這樣容易導致死鎖和競態(tài)條件。
a) avoid manual synchronization because that often leads to deadlocks and race conditions.
2.永遠不要在外部調(diào)用你的同步機制空間。
never call outside your synchronization domain.
3.用回調(diào)方法控制非同步調(diào)用
manage asynchronous call completion on a callback method.
不要等待,調(diào)查 或者阻礙完成。
a) do not wait, poll, or block for completion.
4.總是命名線程。
always name your threads.
線程名字可以在線程調(diào)試窗體里跟蹤,所以做一個調(diào)試調(diào)試線程會使程序變得更有效。
a)name is traced in the debugger threads window, making a debug session more productive.
thread currentthread = thread.currentthread;
string threadname = “main ui thread”;
currentthread.name = threadname;
5.不要在線程中調(diào)用suspend() 或者 resume()
do not call suspend() or resume() on a thread.
6.不要調(diào)用thread.sleep()
do not call thread.sleep().
a)thread.sleep(0) is acceptable optimization technique to force a context switch.
b)thread.sleep() is acceptable in testing or simulation code.
7.不要調(diào)用thread.spinwait()
do not call thread.spinwait().
8.不要調(diào)用thread.abort()來結束線程。
do not call thread.abort() to terminate threads.
不是標記線程的結束,而是應用同步對象去完成。
a) use a synchronization object instead to signal the thread to terminate. see chapter 8 in programming .net components.
9.避免顯示地設定控制執(zhí)行的優(yōu)先權。
avoid explicitly setting thread priority to control execution.
可以基于任務來設定優(yōu)先權,例如用于屏幕存儲器的線程應該低于一般水平。
a) can set thread priority based on task semantic, such as below normal for a screen saver.
10.不要讀取threadstate屬性的value值。
do not read the value of the threadstate property.
應用方法thread.isalive()來判斷線程是否失效。
a) use thread.isalive() to determine whether the thread is dead.
11.應用程序結束時,不要通過設置線程類型來設定線程。
do not rely on setting the thread type to background thread for application shutdown.
可以用watchdog或其他監(jiān)控工具來堅決的結束線程。
a) use a watchdog or other monitoring entity to deterministically kill threads.
12.不要應用線程的本地存儲,除非該線程的相關性已經(jīng)得到保證。
do not use thread local storage unless thread affinity is guaranteed.
13.不要調(diào)用thread.memorybarrier()。
do not call thread.memorybarrier().
14.在未做檢驗之前萬不可調(diào)用thread.join()。
never call thread.join() without checking that you are not joining your own thread.
void waitforthreadtodie(thread thread)
{
debug.assert(thread.currentthread.gethashcode() != thread.gethashcode());
thread.join();
}
15.總是應用lock()聲明,而不是用明顯的monitor manipulation。
always use the lock() statement rather than explicit monitor manipulation.
16.總是將lock()聲明放在它所保護的對象里面。
always encapsulate the lock() statement inside the object it protects.
public class myclass
{
public void dosomething()
{
lock(this)
{…}
}
}
a)可以使用同步方法來代替你自己寫lock()聲明。
can use synchronized methods instead of writing the lock() statement yourself.
17.避免分散鎖定。
avoid fragmented locking(see chapter 8 of programming .net components).
18.避免用監(jiān)視器去等待或刺激對象。應該用手工的或自動重起事件。
avoid using a monitor to wait or pulse objects. use manual or auto-reset events instead.
19.不要使用不穩(wěn)定變量。鎖定對象或域,而不是去保證決定性和線程安全訪問。
do not use volatile variables. lock your object or fields instead to guarantee deterministic and thread-safe access.
不要使用thread.volatileread(), thread.volatilewrite()或者不穩(wěn)定修改器。
a) do not use thread.volatileread(), thread.volatilewrite() or the volatile modifier.
20.永遠不要stack聲明lock,因為這樣不提供自動鎖定。要使用waithandle.waitall()。
never stack lock statements because that does not provide automatic locking. using waithandle.waitall() instead.
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();
obj1.dosomething();
obj1.dosomething();
}