[fpc-pascal]Crashes with multithreading

Andrey Kubyshev email at vtc.ru
Mon Dec 11 13:25:19 CET 2000


Hello Programmers,

Already 2 months i try to write simples stable WEB server for linux. But without
any luck yet. It crashes by some magic reasons. I dont think that i'm
newbie programmer i already wrote this web server in c++ and it works
perfectly, but unfortunately, i need it in pascal.Becouse webserver is
only part of big project. Please see following program and think why
it leak 536 bytes. Becouse i think that my web server (based on
pthreads) crashes coz of invisible memory leaks. I compiled this
example with latest official version 1.0.2 and latest sources from
CSV. I choosed pthreads becouse to complitely free memory in fpc i
must call .Destroy, but i cant call it from inside thread. So , with
pascal hreads , i loose 4 Kb  every thread. Maybe i dont understand
something here in that moment? With pthreads its all easier.


gzipped pthreads.pas is attached
here is dummy program with emulate some work :

program testthreads;

{$mode objfpc}

uses

heaptrc,lineinfo,classes,sysutils,pthread;

procedure usleep(delay:longint);cdecl;external 'libc.so.6';

type

dummy= class
a:array[0..1000] of char;
fake:integer;
public
constructor Create;
destructor Destroy;override;
procedure setfake;
end;

var
number:integer;

constructor dummy.create;
begin
inherited create;
end;

procedure dummy.setfake;
begin
fake:=1;
end;

destructor dummy.destroy;
begin
//writeln(format('destroy object %d',[number]));
inc(number);
inherited destroy;
end;

//thread body
procedure Execute2(i:integer);
var
co:integer;
a:dummy;
s:string;
begin
        a:=dummy.create;
        // writeln('created');
        co:=10;
        // some work emulation
        while (co>0) do begin
                   usleep(1000);
                   a.setfake;
                   s:=s+'+';
                     dec(co);
        end;
        a.destroy;
end;


procedure Execute(i:integer);
begin
        Execute2(i);        // I do it that way, coz pthread_exit happens before
                            // stack vars clear, so we should not have any stack vars
                            // here
        pthread_exit(nil);
end;

var
attr:pthread_attr_t;
threadid:integer;
const
c:integer=200;
// We going to create some threads.
begin
        while(c>0) do begin
                        dec(c);
                        // Setting attributes to detached state
                        pthread_attr_init(@attr);
                        pthread_attr_setdetachstate(@attr,PTHREAD_CREATE_DETACHED);
                        pthread_create(@threadid, @attr,__START_ROUTINE_T(@Execute),1);
                        usleep(10);
        end;
        usleep(8000000); // some time to wait until all treads finished
end.


-- 
Best regards,
Kubyshev Andrey                          mailto:email at vtc.ru
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pthread.pas.gz
Type: application/x-gzip
Size: 2811 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20001211/7453fe19/attachment.bin>


More information about the fpc-pascal mailing list