Solved

can do 64 threads, but not 65.....

Posted on 2002-07-29
10
181 Views
Last Modified: 2010-04-04
Thanks to all who helped me with threads the last few weeks, I got my app up and running and it's working pretty good... but I've hit a new and strange snag:

Seems everything works fine when I have the number of threads less than or equal to 64, but as soon as I set it to 65 or over, I get an I/O Error 104, and the debugger points to the "while not eof(tinfile) do" line of this section of code in my "worker" thread:

  while not eof(tinfile) do
  begin

   {* Start of Critical Section *}

    EnterCriticalSection(CritSect);

    readln(tinfile, thinrec);
tbytes_processed:=tbytes_processed+length(thinrec)+2;
    thperc:=Form1.calc_status(ttotsize_infile, tbytes_processed);
    if thperc >= tprevhperc+1 then
    begin
      tprevhperc:=thperc;
      Form1.gauge2.progress:=thperc;
      Form1.refresh;
    end;

    LeaveCriticalSection(CritSect);

    etc. etc.....



I'm using a "WaitForMultipleObjects" statement to wait for all my threads to finish, before proceeding. I've got a feeling the problem might lie in there....when I execute my app outside of the IDE, it *immediately* finishes in the blink of an eye (no error 104 comes up). Which leads me to believe 65 threads or greater causes a problem for the "WaitForMultipleObjects" cause it seems to fly right past it (...65 is a very suspicious number to fail at). Here's my exact code for that part of my app:


procedure TWaitThr.Execute;
var
  hndlArr : Array[0..9999] of THandle;
  thrArr : Array[0..9999] of TWorkerThread;
  I, dnsx : Integer;
begin
  FreeOnTerminate := True;

  for I := 0 to Form1.SpinEdit3.Value-1  do
  begin
    thrArr[I]:= TWorkerThread.Create(True);
    thrArr[I].Resume;
    hndlArr[I] := thrArr[I].Handle;
  end;
  WaitForMultipleObjects(Form1.SpinEdit3.Value, @hndlArr, True, INFINITE);
  Synchronize(UpdateLabel);
end;



...I call TWaitThr.Create from my main portion of my app. TWaitThr in turn calls and creates my "worker" threads.... then it waits.

Thanks!
   Shawn

P.S: I'm using D6 Enterprise version.
0
Comment
Question by:aztec
  • 6
  • 4
10 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 7187250
The documentation sais this about the "nCount" parameter of WaitForMultipleObjects:

"nCount

Specifies the number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS."

And guess what MAXIMUM_WAIT_OBJECTS is like? Yes, you guessed right, it's 64...   :-)

Regards, Madshi.
0
 

Author Comment

by:aztec
ID: 7187272
ahhhh! OK Madshi thanks!

But is there any way around this? Perhaps another way to "wait" ? I'd like to be able to use more than 64 threads!

Ciao
   Shawn
0
 

Author Comment

by:aztec
ID: 7187276
ahhhh Ok Madshi thanks!But is there any way around this? Perhaps a different approach to use for "wait"? I'd like to be able to use more than 64 threads.

Ciao
   Shawn
0
 

Author Comment

by:aztec
ID: 7187278
(sorry for the double post.... it had looked like my first didn't go thru, and I re-posted)
0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 7187298
Hmmm... Are you waiting until ONE of the threads is finished or until ALL of the threads are finished? If you wait until ALL of the thread are finished, you can simply call WaitForMultipleObjects with 64 threads, then another time with the next 64 threads and so on. If you wait until ONE thread is finished, it all gets more complicated.

Let me ask: For what purpose do you need so many threads (as you can see Microsoft didn't think that so many threads make sense)? And all the threads you are waiting for are your own threads, right? You could put a PostMessage call at the end of each thread, which notifies the main thread about the terminating of the thread. This way you could have a simple message loop in the main thread instead of calling WaitForMultipleObjects.

Regards, Madshi.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:aztec
ID: 7187314
I am waiting for ALL the threads to finish before I proceed (and yes, they are all my own threads). I had *thought* about using more than one WaitForMultipleObjects statement...so this will work then?

Why so many threads? Just to try to get the *fastest* execution out of my app! Overkill? Maybe! :-)

Shawn
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7187327
>> Why so many threads? Just to try to get the *fastest* execution out of my app! Overkill? Maybe! :-)

Well, maybe with so many threads it will end up being slower than with only 1 thread!

Look, a thread can't do magic. Most PCs only have one CPU in it. And your program runs fastest if it uses all the CPU power. One thread can do that already, >64 threads can not make the CPU run at 110% of its power, that's not possible. So what do >64 threads bring? It will have one disadvantage: Namely the thread switching costs performance. You can say e.g. 5% of the CPU goes lost just by switching all your threads. It has one advantage, though: If the threads sometimes are waiting for something, e.g. for data to arrive at the COM port or such stuff, then threads are very good. If you had only one thread in such a situation, it would wait for the data, and the CPU would go to sleep. But when doing file stuff, multiple threads might do more damage than help. You should do some performance tests.

But back to your problem:

Let's say you have 100 threads running. If you now want to wait for ALL threads to be finished, this is exactly the same as first waiting for 64 threads to be finished, and then waiting for the remaining 36 threads to be finished. So you easily call WaitForMultipleObjects as often as you need to wait for all your threads. No problem.

Regards, Madshi.
0
 

Author Comment

by:aztec
ID: 7187555
OK, I will try the multiple "WaitForMultipleObjects" statements. Madshi, would this be the way to do it if I allow a maximum of 128 threads (user enters # of threads in Form1.SpinEdit3):


if Form1.SpinEdit3.Value <= 64 then
     WaitForMultipleObjects(Form1.SpinEdit3.Value, @hndlArr, True, INFINITE)
  else if (Form1.SpinEdit3.Value > 64) and (Form1.SpinEdit3.Value <= 128) then
  begin
    WaitForMultipleObjects(64, @hndlArr, True, INFINITE);
    WaitForMultipleObjects(Form1.SpinEdit3.Value-64, @hndlArr[64], True, INFINITE);
  end;


..does this look correct to you? I am wondering about the "@hndlArr[64]". Is this the correct way to do it, so it will start with the the 65th element of hndlArr?

Regarding hndlArr:
(1) Do I need brackets around it, like this :
 @(hndlArr[64]) ?
(2) Should I initialize all elements of the hndlArr first before using it? If so, what do I initialize it with ... a zero?

Ciao!
   Shawn
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7187590
Looks alright on a quick look.

(1) No brackets are needed.
(2) Initializing is safer, you should initialize with zero.

Regards, Madshi.
0
 

Author Comment

by:aztec
ID: 7188778
thanks Madshi - works fine!

Ciao
  Shawn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now