Delphi 6/7-what book to read about threads and their allowed number

You need to quickly raise your knowledge of a fairly new Delphi 6 or 7 environment, in general, I worked with it, but you need to understand working with threads more deeply.

  1. It is not entirely clear in which cases you need to use Synchronize, or rather with when working with what, and with what not.

  2. Also, the call Terminate it only passes a parameter to the object, but it does not stop the flow, right? Or does it still stop you? I understand that is the check inside the flow procedure written to the Terminated parameter, or is it optional?

  3. And perhaps the main point, well, how to calculate the optimal number of threads and priorities for them for modern PCs, because PCs are not the same as in the early 2000s? Maybe there is some method.

Another Delphi is not needed, some of the modules are already there, and in general, what we planned to finalize is almost ready - the question is that in my opinion, the computer program suspends it.

In general, we write a bot (or rather, we add it), there is already image recognition on the screen and with the voice of the chips, all this must be broken down in streams.

Author: Kromster, 2020-06-16

2 answers

  1. Synchronize - used to execute code in the application's UI thread.

    When to use Synchronize:

    • if you need to refer to a user interface component: mandatory when writing data (Label1.Caption := 'Text'), but often when reading it too (depends on the specific components and the data that comes from them); in general, you should assume by default that all user interface components do not support multithreaded access;
    • if you need to access some object / variable that does not support secure multithreaded access: in this case, you "stack" all read and write operations in the UI thread (not optimal, but acceptable).
  2. Terminate - here you correctly understood: the variable FTerminated := True is simply set.

    If you have studied C#, then there is CancellationToken. Here, in Delphi, the principle is similar - to signal the stream code that it is necessary end early.

    For the Terminate method to work, the thread code must periodically check the FTerminated member during execution, and if it is equal to True, terminate the work ahead of time. This behavior is easily implemented if you have a loop or some step-by-step work (check after each iteration or step on FTerminated, and if True - then do Exit in the TMyOwnThread.Execute(); method).

    Keep in mind that the forced termination of threads by the API function TerminateThread is considered an undesirable practice (they may problems may occur, such as memory leaks), and it is recommended to avoid it. Usage Terminate/FTerminated here it can help, although the programmer has the right to implement their own methods of such "signaling".

  3. The number of threads is a complex question. Everything strongly depends on the specific tasks to be solved.

    When you perform long-term parallel data processing, you should probably limit yourself to the number of processor cores visible in the system. If you want to to ensure that there are no UI hangups, you can play around with affinity thread masks so that none of the worker threads are running on the same core as the UI thread. In Delphi 6/7, this is not implemented, so you will have to bow to the Win32 API: SetThreadAffinityMask

    When you perform small tasks that work quickly, in 1-2 quanta of CPU time, you can run at least hundreds of such threads in one sitting. There will be overhead for creating and destroying threads the system, but up to a certain threshold it will not be noticeable.

    If you perform small tasks in thousands and tens of thousands, then instead of creating a new thread for each task, it is better to implement your own thread pool. Like how Task is made in. NET. If memory does not fail, in .NET, the default pool uses the number of threads 2 times more than there are in the system of processor cores, but there is a documented recommendation to programmers - to perform only "short" (by default) threads in the pool. CPU time) of the task.

 1
Author: velial, 2020-06-17 09:52:01
  1. Synchronize is used to work with VCL components, and also prevents multiple threads from accessing 1 Component at the same time.

  2. Terminate terminates the thread, freeing up resources, and passes the true Terminated property at the end. If you only need to execute the code and terminate the thread, use FreeOnTerminate, for more serious tasks, when the thread needs to be executed before a certain time, then use Terminate

  3. By calculation threads here everything is individual, it all depends on the complexity of the task, if stupidly in the program there is no limit on the number of threads, then we monitor those resources that use threads, the load on the CPU, memory, disk, etc.

 -1
Author: uberchel, 2020-06-17 08:19:22