这就是中文msdn

原创 2007年09月30日 11:51:00

这是中文的msdn来自ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxadvance/html/e51988e7-7f4b-4646-a06d-1416cee8d557.htm

一般性建议

使用多线程时要考虑以下准则:

  • 不要使用 Thread.Abort 终止其他线程。对另一个线程调用 Abort 无异于引发该线程的异常,也不知道该线程已处理到哪个位置。

  • 不要使用 Thread.SuspendThread.Resume 来同步多个线程的活动。不要使用 MutexManualResetEventAutoResetEventMonitor

  • 不要从主程序中控制辅助线程的执行(如使用事件),而应在设计程序时让辅助线程负责等待任务,执行任务,并在完成时通知程序的其他部分。如果辅助线程不阻止,请考虑使用线程池线程。Monitor.PulseAll 在辅助线程阻止的情况下会很有用。

  • 一定要确保已进入监视器的线程始终离开该监视器,即使当线程在监视器中时发生异常也是如此。C# 的 lock 语句和 Visual Basic 的 SyncLock 语句可自动提供此行为,它们用一个 finally 块来确保调用 Monitor.Exit。如果无法确保调用 Exit,请考虑将您的设计更改为使用 Mutex。Mutex 在当前拥有它的线程终止后会自动释放。

  • 一定要针对那些需要不同资源的任务使用多线程,避免向单个资源指定多个线程。例如,任何涉及 I/O 的任务都会从其拥有其自己的线程这一点得到好处,因为此线程在 I/O 操作期间将阻止,从而允许其他线程执行。用户输入是另一种可从专用线程获益的资源。在单处理器计算机上,涉及大量计算的任务可与用户输入和涉及 I/O 的任务并存,但多个计算量大的任务将相互竞争。

请参见

再看看英文的

General Recommendations

Consider the following guidelines when using multiple threads:

  • Don't use Thread.Abort to terminate other threads. Calling Abort on another thread is akin to throwing an exception on that thread, without knowing what point that thread has reached in its processing.

  • Don't use Thread.Suspend and Thread.Resume to synchronize the activities of multiple threads. Do use Mutex, ManualResetEvent, AutoResetEvent, and Monitor.

  • Don't control the execution of worker threads from your main program (using events, for example). Instead, design your program so that worker threads are responsible for waiting until work is available, executing it, and notifying other parts of your program when finished. If your worker threads do not block, consider using thread pool threads. Monitor.PulseAll is useful in situations where worker threads block.

  • Don't use types as lock objects. That is, avoid code such as lock(typeof(X)) in C# or SyncLock(GetType(X)) in Visual Basic, or the use of System.Threading.Monitor.Enter(System.Object) with Type objects. For a given type, there is only one instance of System.Type per application domain. If the type you take a lock on is public, code other than your own can take locks on it, leading to deadlocks. For additional issues, see Reliability Best Practices.

  • Use caution when locking on instances, for example lock(this) in C# or SyncLock(Me) in Visual Basic. If other code in your application, external to the type, takes a lock on the object, deadlocks could occur.

  • Do ensure that a thread that has entered a monitor always leaves that monitor, even if an exception occurs while the thread is in the monitor. The C# lock statement and the Visual Basic SyncLock statement provide this behavior automatically, employing a finally block to ensure that Monitor.Exit is called. If you cannot ensure that Exit will be called, consider changing your design to use Mutex. A mutex is automatically released when the thread that currently owns it terminates.

  • Do use multiple threads for tasks that require different resources, and avoid assigning multiple threads to a single resource. For example, any task involving I/O benefits from having its own thread, because that thread will block during I/O operations and thus allow other threads to execute. User input is another resource that benefits from a dedicated thread. On a single-processor computer, a task that involves intensive computation coexists with user input and with tasks that involve I/O, but multiple computation-intensive tasks contend with each other.

  • Consider using methods of the Interlocked class for simple state changes, instead of using the lock statement (SyncLock in Visual Basic). The lock statement is a good general-purpose tool, but the Interlocked class provides better performance for updates that must be atomic. Internally, it executes a single lock prefix if there is no contention. In code reviews, watch for code like that shown in the following examples. In the first example, a state variable is incremented:

 
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Soma, Jason, ScottGu等的MSDN中文博客

微软开发工具部门的老大都有了自己的MSDN中文博客,这是由微软专门的团队负责把他们的MSDN英文博客及时翻译为中文,以方便大家了解微软开发工作的最新动态和学习新技术,欢迎大家多去看看,并多提宝贵意见,...

win7 简体中文旗舰版 MSDN官方原版

Windows 7 With Sp1 简体中文旗舰版 (MSDN官方原版) ===========================================================...

分享:MSDN visual studio 2010简体中文旗舰版,专业版下载(内置正版密钥)

Visual Studio是微软公司推出的开发环境,是一套完整的开发工具集,用于生成 ASP.NET Web 应用程序、XML Web Services、桌面应用程序和移动应用程序。是目前最流行的Wi...

微软MSDN中文(简体)全下载

产品类型: Applications 产品名称: Office 2007 名称: 2007 Microsoft Office Suite Service Pack 2 (x86) - (Chine...

MSDN放出了VS2010简体中文正式版(附下载地址)

美国当地时间五月26日,微软已经在MSDN上放出VS2010简体中文版供订阅用户下载。相关信息如下: Visual Studio 2010 Ultimate (x86) - DVD (Chine...

VC中文MSDN

  • 2014-12-19 10:34
  • 2.88MB
  • 下载

原来这就是我们买不起房的原因!

近日,一名合肥购房者购买了44套房,拿到了80万元的补贴。据称该名合肥购房者所购买的44套房子全部位于同一小区,位于六安市裕安区城南镇,售楼网站显示该楼盘在售均价为4000元。消息一传出就被各大新闻媒...

MFC MSDN中文帮助

  • 2013-04-17 10:31
  • 6.08MB
  • 下载

vb6.0msdn中文精简版

  • 2009-07-21 18:23
  • 13.48MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)