I have some operations which are based on TThreads. Now I need to create the thread containing the list of jobs to be done, then firing each one as soon as the previous finishes… How should I write it? I can’t allow the threads to be ran simultaneously as there might be over 10 000 operations to be done.
It is quite hard to find documented examples of TEvent and other syncing objects…
Hope I’ll find some help here …
Thanks in advance,
michal
Don’t base your operations on threads. This is the wrong design. Instead you should create a base class for your operation, which exposes a method to perform the operation. Write descendant classes to implement the concrete operations. Don’t make any assumptions about thread contexts, alway use critical sections or similar synchronization objects to protect access to shared resources. More importantly, try to avoid shared resources, or at least try to make shared resources read-only, so that locking isn’t necessary.
With that design in place it becomes possible to perform each operation in the VCL thread by calling the operation method directly, to use a
TThreaddescendant class to perform an operation in its own thread (what you seem to have now), or to schedule all operations on a thread pool. The number of threads in the pool can be adjusted at runtime to match the nature of the operations (processor-bound or I/O-bound) and the number of processor cores the system has. And to answer your question: it is even possible to completely serialize the operations by forcing the pool to use a single thread. Basically you can completely change the way your operations are performed without changing them at all.