Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Threads

Posted on 2003-03-08
16
Medium Priority
?
302 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:MikeMonroe
  • 6
  • 3
  • 3
  • +4
16 Comments
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8093349
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;
0
 

Author Comment

by:MikeMonroe
ID: 8093934
I want to run the threads simultaneously.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8093949
they are runned in the same time as you see ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:MikeMonroe
ID: 8094096
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.
0
 

Author Comment

by:MikeMonroe
ID: 8094098
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.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8094110
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...
0
 

Author Comment

by:MikeMonroe
ID: 8094143
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.
0
 
LVL 1

Expert Comment

by:jturpin
ID: 8095107
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.


0
 
LVL 3

Expert Comment

by:fibdev
ID: 8095566
As jturpin has indicated...

Windows swaps threds in and out of the processor One processor can process one thread at a time.  Sorry :(
0
 
LVL 3

Expert Comment

by:fibdev
ID: 8095573
See "Threads" in the Windows SDK help files that come with Delphi.
0
 

Author Comment

by:MikeMonroe
ID: 8095608
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.
0
 
LVL 3

Expert Comment

by:fibdev
ID: 8095681
Hey I know a guy named Mike Monroe.  What state are you from Mike?
0
 

Author Comment

by:MikeMonroe
ID: 8097213
It's just a nickname...
0
 
LVL 6

Accepted Solution

by:
swift99 earned 90 total points
ID: 8109837
You will never gain speed by threading unless you have ((multiple CPU's) or (processes that are mostly idle)) and (do not tie up the same resources).

Thread switching is a very expensive process in terms of processor performance so it is not done lightly.

BitBlt is very CPU intensive, so even if your threads did get launched at the same instant (impossible on a vonNeuman architecture), you can only gain performance up to the number of CPU's on your box, provided some other resource (the video card) is not tied up by the process.

It sounds like you may need to look directly at the interface API for the graphics adapter in question and address it at a deeper level than the Delphi facade to the Windows API.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 9003331
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.
0
 

Expert Comment

by:CleanupPing
ID: 9316892
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question