Do I understand correctly that ADO objects need to be created not in the stream constructor but in the Execute flow? And why exactly? And where exactly to read about this in more detail?
2 answers
Although it is theoretically possible to create in one thread and use it in another, it is not worth using this technique unless it is explicitly stated that the methods of the object are thread safe. There are several reasons:
- One thread does not know what is being done in the other now unless synchronization tools are used (critical sections / mutexes / events, etc.). Thus, referring to the object created in the main thread in the secondary stream, one can get an “access conflict” when the same memory area is simultaneously modified by both streams. Ultimately, this can lead to memory corruption with all the consequences. Especially indicative in this regard is the work with strings.
- You know nothing about the insides of an object. Even if "outside" it looks single-threaded, it’s not a fact that such a rule applies to its insides. It is quite possible that additional threads are created inside it to perform actions, and events are called via synchronization tools. Thus, by creating an object in the main thread, and trying to use it in the secondary you will receive events in the context of the main thread and this will lead to item 1. In addition, an object can create windows and perform some actions by sending window messages. In the case of a method call by you, this will be thread-safe, and in the event of an event being excited in this way, it will not. True, in order for a window object to work in a stream, in many cases it is necessary to create a message queue in this stream.
- And the most important thing. There is at least an unspoken rule (although it may be documented): where they were created, they were used there. There are exceptions, of course, if the property / method is indicated as thread-safe, or if the event description contains the entry "called in the context of flow XXX".
In the case of documentation on ADO components, there is an explicit indication that some events will be triggered in additional threads if the AsyncExecute, AsyncFetch flags are specified.
You can read, for example, MSDN
|
TADOQuery
, for example, is wonderfully created in the stream designer and works very well.
|