简体   繁体   English

Delphi(Indy)线程安全类

[英]Delphi (Indy) Threadsafe classes

Let's say I have some class, TMaster, which asa field includes a TIdTCPServer. 假设我有一个类TMaster,它的一个字段包括TIdTCPServer。 Some method of the TMaster class is responsible for the OnExecute event of the TIdTCPServer. TMaster类的某些方法负责TIdTCPServer的OnExecute事件。

Firstly, is this threadsafe and acceptible? 首先,该线程安全并且可以接受吗? Secondly, let's assume my class has many other private fields (Name, Date, anything...) can the OnExecute event - which is really a method INSIDE the TMaster class, write to these variables safely? 其次,假设我的班级还有许多其他私有字段(名称,日期,其他任何内容),OnExecute事件(实际上是TMaster类内部的一种方法)可以安全地写入这些变量吗?

I guess I mean to ask if private fields are threadsafe in this situation? 我想问一下在这种情况下私有字段是否是线程安全的?

I am really new to threading and any help will be greatly appreciated! 我真的是线程新手,任何帮助将不胜感激!

Thanks, Adrian! 谢谢,阿德里安!

The way I approach this is not to have the fields used by the events belong to the TidTCPServer owner, but define a custom TidContext descendant and add the fields to that class. 我采用的方法不是让事件使用的字段属于TidTCPServer所有者,而是定义一个自定义的TidContext后代并将这些字段添加到该类中。

Then you simply set the ContextClass property on the server class to the type of the of your custom context. 然后,您只需将服务器类上的ContextClass属性设置为自定义上下文的类型。 This way each Connection/Thread will get its own custom context containing its own private fields, this way there is no issue with concurrent thread access to the same fields. 这样,每个连接/线程将获得包含其自己的私有字段的自己的自定义上下文,这样,并发线程访问相同字段就不会出现问题。

if you have a list of objects that need to be accessed by the different contexts you have two options. 如果您有需要通过不同上下文访问的对象列表,则有两个选择。

1) create copies the objects and store them in a private field in for each context instance This can be done in the OnConnect Event. 1)创建对象的副本并将其存储在每个上下文实例的专用字段中,这可以在OnConnect事件中完成。

2) Protect the objects from concurrent thread access using a synchroniser eg TIdCriticalSection , TMultiReadExclusiveWriteSynchronizer or semaphore, 2)使用同步器(例如TIdCriticalSectionTMultiReadExclusiveWriteSynchronizer或信号量)保护对象免受并发线程访问的影响,

which method you use depends on each individual situation. 使用哪种方法取决于每种情况。

if you need to manipulate any vcl components remember this can't safely be done outside the main vcl thread therefore you should create your own tidnotify decendants for this. 如果您需要操纵任何vcl组件,请记住这不能在主要vcl线程之外安全地完成,因此您应该为此创建自己的tidnotify performing this sort of operation using tidsynch can lead to deadlocks when stoping the tidtcpserver if it is in the middle of a vclsynch operation. 如果在tidtcpserver停止于vclsynch操作中间时,使用tidsynch执行这种操作可能导致死锁。

this is just some of what I have learned over the course of a few years using Indy. 这只是我使用Indy几年来学到的一些东西。

TIdTCPServer is a multi-threaded component. TIdTCPServer是一个多线程组件。 No matter what you wrap it in, the OnExecute event will always be triggered in the context of worker threads, one for each connected client, so any code you put inside the handler must be thread-safe. 无论使用哪种包装, OnExecute事件始终将在辅助线程的上下文中触发,每个连接的客户端都触发一个,因此,放入处理程序中的任何代码都必须是线程安全的。 Members of the TMaster class need adequate protection from concurrent access by multiple threads at the same time. TMaster类的成员需要足够的保护,以防止多个线程同时进行并发访问。

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

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