Link to home
Start Free TrialLog in
Avatar of MikeMonroe
MikeMonroe

asked on

Threads

I want to make 4 threads and start them at the same time. Also, I want the threads to release the resources they've used when they finish executing. Then, I wan't to be able to call them again. Thank you.
Avatar of Lukasz Lach
Lukasz Lach

type
  TSomeThread = class(TThread)
    StopThread: boolean;
    procedure Execute; override;
    constructor Create; overload;
  end;

var SomeThread: array[1..4] of TSomeThread;

constructor TSomeThread.Create;
begin
  FreeOnTerminate := True;
  StopThread := False;
  inherited Create(false);
end;

procedure TNetEnumThread.Execute;
begin
  try
    // your thread actions
    if StopThread then ...
  finally
    // free all resources
  end;
end;

procedure TformMain.FormDestroy(Sender: TObject);
var
  nr: byte;
begin
  for nr := 1 to 4 do
  if SomeThread[nr] <> nil then
  begin
    SomeThread[nr].StopThread := True;
    SomeThread[nr].WaitFor;
  end;
end;


To create your threads use:

for nr := 1 to 4 do
SomeThread[nr] := TSomeThread.Create;
Avatar of MikeMonroe

ASKER

I want to run the threads simultaneously.
they are runned in the same time as you see ;-)
Actually, they don't work simultaneously. I tried this and I added some watches and the second thread starts after the first is finished. Please, test your future answers first.
Actually, they don't work simultaneously. I tried this and I added some watches and the second thread starts after the first is finished. Please, test your future answers first.
Please check what you are asking for before...
>> I want to run the threads simultaneously.
Run or work? That is to be a difference isn't it?
My answer is (checked!!!): use CriticalSections...
Ok, so I need the threads to work in the same time. That's what I wanted. Something like the thread sort example in Delphi, but not so complicated.
for the threads to actually "work at the same time" I assume you want the threads to be processing at the same time?

To do this you would need a multi-processor machine. If you want all four to run together you would need four processors.

This is probably NOT what you want. On a single processor machine each thread is given a time slice (approc 12ms in Win98). It processes for this time (unless it has to wait then it gives up its slice). Then the operating system gives a time slice to the next thread. Round and round it goes until the thread finishes processing and/or terminates. For all appearences the threads do run at the same time.

In your example, it simply sounds like each thread does not have enought to do for its entire time slice. So it terminates, and then the next processes etc.


As jturpin has indicated...

Windows swaps threds in and out of the processor One processor can process one thread at a time.  Sorry :(
See "Threads" in the Windows SDK help files that come with Delphi.
I needed to grab two slices(400x200) from the screen in 16-bit format and BitBlt is very slow. I thought that it will be faster with threads(1 for each slide). What can I do? On GeForce this works fine, but on RivaTNT BitBlt is VERY slow.
Hey I know a guy named Mike Monroe.  What state are you from Mike?
It's just a nickname...
ASKER CERTIFIED SOLUTION
Avatar of swift99
swift99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is apples and pears.

The solution provided is correct, but it is not demonstratabley so, since the threads only do CPU, they do NO I/O so they CANNOT overlap. In actual practice this will work for BitBlt since the latter actually does a hardware data transfer into the graphic card, and this frees the CPU for some time. The threads solution only overlaps the CPU processing time for the first access not the second. Note that the graphics driver implements the BitBlt() function, and some drivers do this by CPU others do it on the card, and that will explain why some cards are slow.

PS: This question has been answered and should be PAQed.
MikeMonroe:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.