Delphi: Threaded list of thread jobs - queueing
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 a开发者_如何学Pythons 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 TThread
descendant 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.
I have just implemented something very similar.
I think what you want is a thread-pool system.
You have a thread-pool that contains some number of threads (the maximum number of items that will be executed simultaneously). You then create a work-unit object, and place in a queue. Each worker thread gets the first available queued work unit and executes it. On completion, the worker thread then waits until there are more work-units in the queue.
The advantages of this approach are that you can easily control the maximum number of simultaneous operations and you don't create and discard threads for each operation (which is expensive).
You should look at OmniThreadLibrary. It makes worker threads extremely simple, and you basically just add a task for it to do; each one starts right after the previous one finishes, and the library allows you to easily pass status messages and so forth back to your main thread for UI updates.
Let's say you have a thread which will execute the others, say WorkerThread.
In the WorkerThread you could put the threads to be called in an array of TThread, a TThreadList, TList, basically whichever is more comfortable with you.
Then in a for loop start each of them. Now, since you don't want them to run simoultaneously you have 2 ways to wait for the running thread, either use some flag and listen to the thread's OnTerminate event which when gets fired sets the flag, or use
WaitForSingleObject(Thread.Handle, INFINITE);
I don't fully understand your question and don't really get the point in using multiple threads and then serializing execution...but you could do it like this:
JobList : TList <TThread>;
...
JobList.Add (TMyCustomThread.Create (True)); // Create suspended
JobList.Add (TMyOtherThread.Create (True)); // Create suspended
...
for Thread in JobList do
begin
Thread.Start;
Thread.WaitFor;
end;
This will execute each thread and then wait until the thread finishes before executing the next thread.
The example code assumes that you use D2009 or later (you didn't specify in your question) and that you have created your threads in suspended state. If you use an older Delphi version you have to call Resume
instead of Start
and have to replace the generic TList
with a simple TList
or an array.
Be aware of the memory leaks in the example code.
Generics Collections TQueue could be used as the container for the individual job objects.
A worker thread then would pick up ('extract') the first job and execute it, and then continue until the queue is empty - then it would have to pause until another job has been added to the queue and continue.
You would need to implement thread-safe access to the queue for adding new jobs.
精彩评论