简体   繁体   English

Delphi XE 中的多线程有哪些新功能?

[英]What is new in multithreading in Delphi XE?

I have seen some time ago people discussing about the new multithreading in Delphi XE and about the fact that Delphi has some 'native' problems because of the way it implements multithreading.前段时间我看到人们在讨论 Delphi XE 中的新多线程以及 Delphi 由于实现多线程的方式而存在一些“本机”问题的事实。 And they were recommending some external libraries to replace the default Delphi multithreading.他们推荐了一些外部库来替换默认的 Delphi 多线程。

Can you point me to some documentation and the most popular multithreading library for Delphi XE?你能指点我一些文档和 Delphi XE 最流行的多线程库吗? Thanks谢谢

[You can set thread name: TThread.NameThreadForDebugging .] (implemented in D2010 as David pointed out) [您可以设置线程名称: TThread.NameThreadForDebugging .] (在 D2010 中实现,正如 David 指出的那样)

You can create anonymous thread (a thread that executes anonymous function and doesn't need a TThread descendant object): TThread.CreateAnonymousThread .您可以创建匿名线程(执行匿名函数并且不需要 TThread 后代对象的线程): TThread.CreateAnonymousThread

Delphi threading frameworks are discussed here: Delphi 线程框架在这里讨论:

Also, in addition to what's already been mentioned:此外,除了已经提到的内容:

I think the "native" issues you are talking about are not related to the way TThread is implemented, but to other aspects of the RTL:我认为您所谈论的“原生”问题与TThread的实现方式无关,而是与 RTL 的其他方面有关:

  • The memory manager is very fast and well written, but it fails to scale in a linear way when running with a number of concurrent threads on multiple cores;内存管理器速度非常快并且编写得很好,但是当在多个内核上运行多个并发线程时,它无法以线性方式扩展;
  • Reference-counted types (like string and dynamic arrays ) are implemented with an asm lock opcode to have atomic reference counting (InterlockedDecrement/InterlockedIncrement in x64), which may also scale badly on multi-threaded applications (that is, all cores freezes when this opcode is executed - even if newer CPUs made progress about this, an RCU implementation may scale better).引用计数类型(如string动态数组)使用 asm lock操作码实现以具有原子引用计数(x64 中的 InterlockedDecrement/InterlockedIncrement),这也可能在多线程应用程序上严重扩展(即,所有内核在此时冻结执行操作码 - 即使较新的 CPU 在这方面取得了进展, RCU实现也可以更好地扩展)。

Those weakness are common to all multi-thread libraries - even OTL will suffer about this .这些弱点是共同的多线程库-甚至OTL将遭受大约这个 They do exist since very early Delphi versions, and are still there with Delphi XE2.它们确实存在于早期的 Delphi 版本中,并且在 Delphi XE2 中仍然存在。 The 64 bit implementation is similar (even slower), and the Mac OS platform shares the very same implementation. 64 位实现类似(甚至更慢),Mac OS 平台共享完全相同的实现。

Please see this other SO question about how to write scaling multi-threaded applications in Delphi.请参阅有关如何在 Delphi 中编写缩放多线程应用程序的其他 SO 问题

To be honest, both points above will appear only on some very specific kind of applications.老实说,以上两点只会出现在某些非常特定的应用程序中。

So there is nothing to worry about multi-threading in Delphi, if you know those points, and do not abuse of memory manager calls or string process in your threads.因此,如果您知道这些要点,并且不要在线程中滥用内存管理器调用或字符串进程,那么在 Delphi 中就无需担心多线程。

gabr probably mentioned main new additions. gabr 可能提到了主要的新增功能。 What was left probably is the new overloads for TThread.Synchronize and TThread.Queue which can now execute anonymous methods.剩下的可能是TThread.SynchronizeTThread.Queue的新重载,它们现在可以执行匿名方法。

I believe all of newly introduced features were covered already.我相信所有新引入的功能都已经涵盖了。

For the documentation part, here is an archived copy on classic tutorial book Martin Harvey.对于文档部分,这里是经典教程书Martin Harvey的存档副本 Multithreading - The Delphi Way. 多线程 - Delphi 方式。 After reading you will most likely realize what there is no real need for any contributed libraries (except, maybe, the thread pool), remember, frameworks are not simplifying things for you, they also depriving you from the fine-grain control.阅读之后,您很可能会意识到实际上不需要任何贡献的库(线程池除外),请记住,框架并没有为您简化事情,它们还剥夺了您对细粒度控制的权利。

TThreadedQueue was introduced in XE. XE 中引入了 TThreadedQueue。

I find it useful for passing information from worker threads to the main thread or other consumer threads.我发现将信息从工作线程传递到主线程或其他消费者线程很有用。 The last part, having multiple consumer threads, was buggy unfortunately.不幸的是,最后一部分有多个消费者线程。 There was a bug in TMonitor, a class introduced by Delphi 2009, used to synchronize access to the queue in TThreadedQueue. Delphi 2009 引入的一个类TMonitor 有一个bug,用于同步访问TThreadedQueue 中的队列。

This has been fixed for XE2.这已为 XE2 修复。 See tthreadedqueue-not-capable-of-multiple-consumers请参阅tthreadedqueue-not-capable-of-multiple-consumers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM