Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 268
migajek Avatar asked Nov 26 '09 21:11

migajek


4 Answers

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.

like image 113
mghie Avatar answered Sep 29 '22 09:09

mghie


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).

like image 39
Alistair Ward Avatar answered Sep 29 '22 09:09

Alistair Ward


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.

like image 23
Ken White Avatar answered Sep 29 '22 07:09

Ken White


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);
like image 24
zz1433 Avatar answered Sep 29 '22 07:09

zz1433