Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-05

Strange issue with threads

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

While I was testing an unrelated issue, I discovered this: under windows, with fpc 2.2.4, I try to start 10000 thread (I know it's absurd but it's just a test) and have all of them linger around for 10 seconds. The thread actually started are 121. The problem is that no exception is raised, so how can I be sure that a thread is really started in a more normal scenario? Is there a hard limit on the number of threads under windows? If so, why no exception when I try to create more? If I run the same program under Linux, an "EThread: failed to create new thread" exception is raised after around 300 threads.

This is the test program

---------------8<-----------8<-------------8<------------------
program project1;
{$mode objfpc}{$H+}
uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes, Sysutils, SyncObjs
   { you can add units after this };
var
   Started:integer;
   AccessStarted:TCriticalSection;
type
   { TMyThread }
   TMyThread = class(TThread)
     procedure Execute;override;
   end;
procedure TMyThread.Execute;
begin
   AccessStarted.Acquire;
   Started:=Started+1;
   AccessStarted.Release;
   Sleep(10000);
end;
var i:integer;
{$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
begin
   AccessStarted:=TCriticalSection.Create;
   Started:=0;
   for i:=1 to 10000 do
   begin
     TMyThread.Create(false);
     writeln(i);
   end;
   while true do
   begin
     AccessStarted.Acquire;
     writeln('Threads started:',started);
     AccessStarted.Release;
     Sleep(1000);
   end;
end.

---------------8<-----------8<-------------8<------------------

If I remove the sleep(10000) (the writeln(i) should also be removed), the number of threads started is a random
number, normally less than the 10000 expected, hence my deduction that there's a hard limit on the number of threads (i.e. less threads hanging around so more can be actually started).

Bye

Luca

Reply by email

Jonas Maebe
to Luca Olivetti
Luca Olivetti wrote 9 lines
While I was testing an unrelated issue, I discovered this: under windows, with fpc 2.2.4, I try to start 10000 thread (I know it's absurd but it's just a test) and have all of them linger around for 10 seconds. The thread actually started are 121. The problem is that no exception is raised, so how can I be sure that a thread is really started in a more normal scenario?

You can obtain the "handle" property of the tthread instance and check it against whatever "invalid handle" CreateThread returns under Windows when it fails (although that will obviously not be portable).

Is there a hard limit on the number of threads under windows?

I'm pretty sure there is one for every OS.

If so, why no exception when I try to create more?

Because the Windows version of tthread.create does not check for any errors. You can submit a bug report about this.

If I run the same program under Linux, an "EThread: failed to create new thread" exception is raised after around 300 threads.

The unix version does check for errors.

Jonas

Reply by email

rzbhatti
to Luca Olivetti

I followed the bug report
http://bugs.freepascal.org/view.php?id=13798
and it says that it is resolved and closed.

I am using fpc 2.4.4 on Windows XP. Like Luca Olivetti I am trying to create 256 threads, but it chocks at 121 saying :

An unhandled exception occurred at $0041099C :
EThread : Failed to create new thread, code:8

I could not find the details of the actual resolution of this bug. Please help...

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Strange-issue-with-threads-tp2823464p4728208.html Sent from the Free Pascal - General mailing list .

Reply by email

Luca Olivetti
to rzbhatti

Al 23/08/11 22:46, En/na rzbhatti ha escrit:

Show quoted text 12 lines
I followed the bug report http://bugs.freepascal.org/view.php?id=13798 and it says that it is resolved and closed. I am using fpc 2.4.4 on Windows XP. Like Luca Olivetti I am trying to create 256 threads, but it chocks at 121 saying : An unhandled exception occurred at $0041099C : EThread : Failed to create new thread, code:8 I could not find the details of the actual resolution of this bug. Please help...

The bug is actually solved: now you see an exception when a thread cannot be created (before the bug was solved you couldn't know if the thread was successfully created or not).

As per the solution to the maximum number of threads, you can look here:

http://stackoverflow.com/questions/481900/whats-the-maximum-number-of-threads-in-windows-server-2003

it seems the limit is due to the available memory and the stack used by each thread.

Bye

Luca

Reply by email

Werner Van Belle
to rzbhatti: I followed the bug report http://bugs.freepascal.org/view.php?id=13798 and it says that…
rzbhatti wrote 6 lines
I followed the bug report http://bugs.freepascal.org/view.php?id=13798 and it says that it is resolved and closed. I am using fpc 2.4.4 on Windows XP. Like Luca Olivetti I am trying to create 256 threads, but it chocks at 121 saying :

Euhm ? You are one of the authors of linux' console-tools or so ? You know, that little 'daemon that does nothing' but spawns 64 processes, just to be sure.
If you are not: why do you need 255 threads ???

Werner,-

Reply by email