program code;

{$mode objfpc}{$H+}

uses cthreads, Classes, SysUtils;
  
type
  TTestThread = Class(TThread)
  protected
    procedure Execute; override;
  public
    constructor Create;
  end;

  TMainThread = Class(TThread)
  protected
    procedure Execute; override;
  end;

procedure TMainThread.Execute;
Var
  I : Integer;
begin
  try
    for I := 0 to 10000 do  //after 381 Threads
      TTestThread.Create;   //rais Exception with "Failed to create new Thread"
  except
    on E: Exception do WriteLn(E.Message);
  end;
  Sleep(1000);
end;

constructor TTestThread.Create;
begin
  FreeOnTerminate := True;
  inherited Create(False);
end;

procedure TTestThread.Execute;
begin //Do nothing
end;


var
  aMainThread : TMainThread;
begin
  aMainThread := TMainThread.Create(false);
  aMainThread.WaitFor;
  aMainThread.Free;
end.

