• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

Pool of working threads?

Hi Experts,

Can you give me an example of 'Pool of working threads'?
My gues this is some amount of available threads which could be checked if they are free and then assigned a new task. The thread is not terminated after finished its task, but returned to the pool. This is the best I could explain what I am looking for.

Sincerely,
odissey1
0
odissey1
Asked:
odissey1
  • 3
  • 2
2 Solutions
 
vadim_tiCommented:
You can use suspend method in thread after hi finished its task.

And you can resume it or before terminating task or after you have a new task for it.

procedure TYourThread.Execute;
begin
  while not terminated do begin
     doSomeTask;
// here you can test if you have to much threads
// do not suspend and exit
    Suspend;
  end
end;

procedure TMainForm.NewTaskAvailable;
var
  done : boolean;
begin
  done := false;
  for i:=0 to ThreadList.Count - 1 do
     with TYourThread(ThreadList[i]) do
        if Suspended then begin
           Suspended := false;
          done := true;
           break;
        end
  if not done then
     ThreadList.Add(TYourThread.Create(true));
end;
0
 
shaneholmesCommented:
vadim_ti,

I believe what  odissey1 wants is a queue of threads. Where, one thread comes off the top, gets used, completes it completes its task, instead of getting terminated, it goes to the back of the queue waiting to be used again - then the next one comes off the top - and so on.

Is this  correct odissey1?
0
 
odissey1Author Commented:
To shaneholmes,

Yes, I think you describe it correctly. I've found some TWSocket example in the newsgroup and there it was advised to use a 'pool of working threads' (instead of regulal threading mechanism)  for a particular problem (boosts of high-speed data transfer with slow processing on receiving side). I decided to learn more about this approach and will be glad to get any extra knowlegde about that subject. The best would be some code snippet. I think I could follow from there.

Regards,
odissey1

 
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
shaneholmesCommented:
Would this be for some chat or P2P application?

ShaneHolmes
0
 
shaneholmesCommented:
I don't think older versions of delphi have support for thread pooling. This is something you would have to implement on your own.

In the new Delphi,  new to the TThread class are a couple of methods. These methods are listed below:

    function WaitFor(TimeOut: Integer; var ReturnValue: LongWord): Boolean; overload;
    class procedure Queue(AThread: TThread; AMethod: TThreadMethod); overload;
    class procedure RemoveQueuedEvents(AThread: TThread; AMethod: TThreadMethod);
    class procedure StaticQueue(AThread: TThread; AMethod: TThreadMethod);

In previous versions of Delphi, the WaitFor() method did not allow to specify how long it should wait for the thread to terminate. TThread now adds another overloaded version of WaitFor() that provides a timeout parameter. This new version returns true when the thread terminates. Otherwise, if the timeout occurs, it returns false.

When a worker thread needs to execute a method from the main thread, the worker thread would block, waiting for the main thread to process the request. New to TThread, the Queue method provides the ability to asynchronously call methods in the main thread. By queuing the method instead of blocking, allows the worker thread to continue without waiting for the execution of the method placed on the queue. Eventually the main thread is ready to process the method(s) on the queue, and the entire queue is processed at once.

RemoveQueuedEvents() allows for either a specific method to be removed from the queue, or all of the queued methods for a specific thread to be removed.


ShaneHolmes
0
 
odissey1Author Commented:
Hi All,

I am sorry for delay. Thank you for help

Sincerely,
odissey1
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now