如何选择各种方式做线程德尔福之间? [英] How Do I Choose Between the Various Ways to do Threading in Delphi?

查看:347
本文介绍了如何选择各种方式做线程德尔福之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,我终于得到了实现某种穿入我的德尔福2009年计划。如果只有一个做到这一点的方式,我会关闭和运行。但我看到了几种可能性。

It seems that I've finally got to implement some sort of threading into my Delphi 2009 program. If there were only one way to do it, I'd be off and running. But I see several possibilities.

任何人能解释什么是这些,为什么我会选择一个比另一个的区别。

Can anyone explain what's the difference between these and why I'd choose one over another.


  1. 在Delphi中的TThread类

  1. The TThread class in Delphi

AsyncCalls 通过的安德烈亚斯Hausladen

OmniThreadLibrary 通过普里莫兹Gabrijelcic(贾布尔)

...任何其他方面?

... any others?

编辑:

我刚刚读过贾布尔在2010年3月(10号)帕斯卡杂志上的题为四个方面的优秀文章创建一个线程。你必须订阅获取内容的杂志,所以受版权,我读不出任何实质性约在这里。

I have just read an excellent article by Gabr in the March 2010 (No 10) issue of Blaise Pascal Magazine titled "Four Ways to Create a Thread". You do have to subscribe to gain content to the magazine, so by copyright, I can't reproduce anything substantial about it here.

在总之,贾布尔描述了使用TThreads,直接Windows API调用,安迪的AsyncCalls,和他自己OmniThreadLibrary之间的差异。他并总结在最后的是:

In summary, Gabr describes the difference between using TThreads, direct Windows API calls, Andy's AsyncCalls, and his own OmniThreadLibrary. He does conclude at the end that:

我不是说你要选择什么比经典德尔福的方式(的TThread),否则但它仍然是不错的被告知的选项你有

"I'm not saying that you have to choose anything else than the classical Delphi way (TThread) but it is still good to be informed of options you have"

Mghie的答案是非常彻底的,并建议OmniThreadLibrary可能是preferable。但我仍然有兴趣在每个人的约我(或任何人)应该如何选择适合自己的应用其线程方法的意见。

Mghie's answer is very thorough and suggests OmniThreadLibrary may be preferable. But I'm still interested in everyone's opinions about how I (or anyone) should choose their threading method for their application.

和您可以添加到列表中:

And you can add to the list:

。 4.直接调用Windows API的

. 4. Direct calls to the Windows API

。 5. 米莎Charrett的 CSI分布式应用程序框架在他的回答提出由LachlanG。

. 5. Misha Charrett's CSI Distributed Application Framework as suggested by LachlanG in his answer.

结论:

我可能会去与OmniThreadLibrary。我喜欢贾布尔的工作。我很多年前用他的探查GPProfile,目前我使用他的GPStringHash这实际上是OTL的一部分。

I'm probably going to go with OmniThreadLibrary. I like Gabr's work. I used his profiler GPProfile many years ago, and I'm currently using his GPStringHash which is actually part of OTL.

我唯一的担心可能会被提升它具有64位或Unix / MAC处理工作,一旦Embarcadero公司补充说,功能集成到德尔福。

My only concern might be upgrading it to work with 64-bit or Unix/Mac processing once Embarcadero adds that functionality into Delphi.

推荐答案

如果你不使用多线程,你可能不应该与的TThread 开始,因为它是经历但薄薄的一层在本地线程。我认为它也被周围的边缘有点粗糙;它,因为用Delphi 2的推出还没有进化了很多,大多发生变化,允许在Kylix的时间框架Linux兼容,并纠正了较为明显的缺陷(如固定破碎MREW类,最后去precating 暂停()恢复()在最新的Delphi版本)。

If you are not experienced with multi-threading you should probably not start with TThread, as it is but a thin layer over native threading. I consider it also to be a little rough around the edges; it has not evolved a lot since the introduction with Delphi 2, mostly changes to allow for Linux compatibility in the Kylix time frame, and to correct the more obvious defects (like fixing the broken MREW class, and finally deprecating Suspend() and Resume() in the latest Delphi version).

使用简单的线程包装类基本上也使开发人员集中精力是太低的水平。为了妥善利用多个CPU内核的重点任务,而不是线程比较好,因为线程工作的划分并不很好地适应不断变化的需求和环境 - 这取决于硬件和并行的最佳数量运行的其他软件线程可能甚至在同一系统上的不同的时间变化很大。你只传递工作组块,并调度它们自动地使现有资源得到最佳利用库有很大帮助,在这方面。

Using a simple thread wrapper class basically also causes the developer to focus on a level that is much too low. To make proper use of multiple CPU cores a focus on tasks instead of threads is better, because the partitioning of work with threads does not adapt well to changing requirements and environments - depending on the hardware and the other software running in parallel the optimum number of threads may vary greatly, even at different times on the same system. A library that you pass only chunks of work to, and which schedules them automatically to make best use of the available resources helps a lot in this regard.

AsyncCalls是引入线程到应用程序中的第一步。如果在你的程序从哪几个方面需要进行的是相互独立的一些耗时的步骤,那么你可以简单地通过每个人传递给AsyncCalls执行它们是异步的。即使你只有一个这样的耗时的操作可以异步执行它,只是显示在VCL线程进度界面,可选择允许取消行动。

AsyncCalls is a good first step to introduce threads into an application. If you have several areas in your program where a number of time-consuming steps need to be performed that are independent of each other, then you can simply execute them asynchronously by passing each of them to AsyncCalls. Even when you have only one such time-consuming action you can execute it asynchronously and simply show a progress UI in the VCL thread, optionally allowing for cancelling the action.

AsyncCalls是IMO不是整个程序运行过程中停留在后台工作人员这么好,它可能无法在一些程序中的对象具有线程关联(如数据库连接或可能有一个OLE对象使用要求所有通话发生在同一个线程)。

AsyncCalls is IMO not so good for background workers that stay around during the whole program runtime, and it may be impossible to use when some of the objects in your program have thread affinity (like database connections or OLE objects that may have a requirement that all calls happen in the same thread).

什么你还需要注意的是,这些异步行动的的的发射后不管的一种。每超载 AsyncCall()函数返回,你可能需要将基准存,如果你想将 IAsyncCall 接口指针避免阻塞。如果不保持的基准,则该时刻的引用计数达到零的接口将被释放,这将导致该线程释放接口等待异步调用来完成。这是东西,你可能会看到在调试时,退出创建该方法时, IAsyncCall 可能需要很长时间一个神秘的量。

What you also need to be aware of is that these asynchronous actions are not of the "fire-and-forget" kind. Every overloaded AsyncCall() function returns an IAsyncCall interface pointer that you may need to keep a reference to if you want to avoid blocking. If you don't keep a reference, then the moment the ref count reaches zero the interface will be freed, which will cause the thread releasing the interface to wait for the asynchronous call to complete. This is something that you might see while debugging, when exiting the method that created the IAsyncCall may take a mysterious amount of time.

OTL在我看来是最多才多艺的你三个选择,我会用它没有第二个想法。它可以做的一切的TThread 和AsyncCalls可以做的,加得多。它具有完善的设计,这是高层次都足以使为用户轻松的生活,并让一个端口到Unixy系统(同时保持大部分的接口完好无损)看尽可能少的,如果不容易。在过去的几个月它也开始获得了并行工作的一些高层次的结构,强烈推荐。

OTL is in my opinion the most versatile of your three options, and I would use it without a second thought. It can do everything TThread and AsyncCalls can do, plus much more. It has a sound design, which is high-level enough both to make life for the user easy, and to let a port to a Unixy system (while keeping most of the interface intact) look at least possible, if not easy. In the last months it has also started to acquire some high-level constructs for parallel work, highly recommended.

OTL有几十个样本过,这是非常重要的开始。 AsyncCalls无关,但评论的几行字,但它是很容易理解,由于其有限的功能(它只有一件事,但它确实很好)。 的TThread 只有一个样本,它并没有真正在14年内改变,主要是如何不做事的例子。

OTL has a few dozen samples too, which is important to get started. AsyncCalls has nothing but a few lines in comments, but then it is easy enough to understand due to its limited functionality (it does only one thing, but it does it well). TThread has only one sample, which hasn't really changed in 14 years and is mostly an example of how not to do things.

无论您选择的选项,没有图书馆将不再需要了解线程基础知识。看了这些好书是prerequisite任何成功的编码。例如适当的锁是与他们的要求。

Whichever of the options you choose, no library will eliminate the need to understand threading basics. Having read a good book on these is a prerequisite to any successful coding. Proper locking for example is a requirement with all of them.

这篇关于如何选择各种方式做线程德尔福之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆