Direct use of pthreads to create trivial threads crashes always on powerpc and sometimes on i386
Original Reporter info from Mantis: ppopov99
-
Reporter name: Peter Popov
Original Reporter info from Mantis: ppopov99
- Reporter name: Peter Popov
Description:
I am not sure if this is a bug in the RTL, something with the target OS, or incorrect compiler switches. I am unable to run a simple threaded applications on powerpc. After including cthreads, a simple thread descendent gets created fine, but then the application segfaults.
So I reduced the problem to a simple program, linked against Libc, which calls directly pthread_create and then pthread_join to wait on the thread.
On PowerPC5+ running suse linux this always crashes. On a i386 slackware 11 with 2.4.33 kernel it works most of the time.
This is the code (also attached):
---------------------------------
program phello;
uses libc;
function Hello(arg: pointer): Pointer; cdecl;
begin
writeln('Hello from thread #', PInteger(arg)^);
Hello := nil;
end;
const N = 2;
var
i: Integer;
ret: Pointer;
arg: array[1..N] of Integer;
threads: array[1..N] of TThreadID;//pthread_t;
attr: pthread_attr_t;
begin
IsMultiThread := true;
pthread_attr_init(attr);
for i := 1 to N do
begin
Writeln('Creating thread #',i);
arg[i] := i;
pthread_create(threads[i], attr, @Hello, @arg[i]);
end;
for i := 1 to N do
begin
Writeln('Waiting for thread #',i);
pthread_join(threads[i], ret);
end;
end.
----------------------------------
Steps to reproduce:
compile with "fpc -MDelphi phello.pas"
Additional information:
Output on PowerPC:
----------------------
Creating thread #1
Creating thread #2
Waiting for thread #1
Runtime error 216 at $FFEC5E70
$FFEC5E70
$0FE942B4
Waiting for thread #2
Runtime error 216 at $FFEC5E70
$FFEC5E70
$0FE942B4
Runtime error 216 at $FFEC5E70
$FFEC5E70
$0FE942B4
-----------------------
On i386, it does not always crash. When it does, it gives:
-----------------------
Creating thread #1
Hello from thread #1
Runtime error 101 at $08048868
$08048868
$40036F5B
$40192BEA
Hello from thread #1
Creating thread #2
Runtime error 101 at $080488FA
$080488FA
$08061927
----------------------
Mantis conversion info:
- Mantis ID: 14176
- OS: linux
- Platform: powepc5+, i386
- Version: 2.2.4
- Fixed in version: 2.4.0
- Fixed in revision: 12475 (#bb9e9624)