首页 > 开发 > 综合 > 正文

IDesign C#编码规范(之八)

2024-07-21 02:18:48
字体:
来源:转载
供稿:网友
4.4多线程 multithreading
1.应用同步机制空间。
use synchronization domains. see chapter 8 in programming .net components.
避免手工的同步机制,应为这样容易导致死锁和竞态条件。
a) 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.
不要等待,调查 或者阻碍完成。
a) do not wait, poll, or block for completion.
4.总是命名线程。
always name your threads.
线程名字可以在线程调试窗体里跟踪,所以做一个调试调试线程会使程序变得更有效。
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.不要在线程中调用suspend() 或者 resume()
do not call suspend() or resume() on a thread.
6.不要调用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.不要调用thread.spinwait()
do not call thread.spinwait().
8.不要调用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.避免显示地设定控制执行的优先权。
avoid explicitly setting thread priority to control execution.
可以基于任务来设定优先权,例如用于屏幕存储器的线程应该低于一般水平。
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或其他监控工具来坚决的结束线程。
a) use a watchdog or other monitoring entity to deterministically kill threads.
12.不要应用线程的本地存储,除非该线程的相关性已经得到保证。
do not use thread local storage unless thread affinity is guaranteed.
13.不要调用thread.memorybarrier()。
do not call thread.memorybarrier().
14.在未做检验之前万不可调用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.避免用监视器去等待或刺激对象。应该用手工的或自动重起事件。
avoid using a monitor to wait or pulse objects. use manual or auto-reset events instead.
19.不要使用不稳定变量。锁定对象或域,而不是去保证决定性和线程安全访问。
do not use volatile variables. lock your object or fields instead to guarantee deterministic and thread-safe access.
不要使用thread.volatileread(), thread.volatilewrite()或者不稳定修改器。
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();
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表