?
Solved

Threads

Posted on 2003-03-08
16
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, left to enroll

777 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