国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

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();
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长沙市| 海丰县| 清流县| 宣武区| 中山市| 凤凰县| 邵东县| 繁峙县| 吉隆县| 潜山县| 原阳县| 扎鲁特旗| 西盟| 锦州市| 九江市| 鄂温| 且末县| 卓资县| 松溪县| 缙云县| 台山市| 罗山县| 上犹县| 图们市| 呼伦贝尔市| 萨迦县| 黄山市| 平果县| 河间市| 来宾市| 余江县| 祁阳县| 马边| 和顺县| 大田县| 镇平县| 奈曼旗| 隆昌县| 修水县| 炉霍县| 达州市|