summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/packages/webtbs/tw14265.pp
blob: 33834710a8c25fa2d5c53f2a21f34ec2a6be3bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{ %target=linux }
{ %opt=-Xt }

program phello;
{$linklib c}

{$packrecords c}

uses
  ctypes, unixtype, pthreads;

const N = 2;
var
  res:array[1..N] of Integer;

function Hello(arg: pointer): pointer; cdecl;
begin
//  writeln('Hello from thread #', PInteger(arg)^);
  res[PInteger(arg)^] := PInteger(arg)^;
  Hello := nil;
  pthread_exit(pointer(Hello));
end;

var
  i: Integer;
  ret: Pointer;
  arg: array[1..N] of Integer;
  threads: array[1..N] of TThreadID;
  attr: TThreadAttr;
begin
  Writeln('Testing simple thread creation');
  pthread_attr_init(@attr);
  for i := 1 to N do
  begin
    Writeln('Creating thread #',i);
    arg[i] := i;
    if pthread_create(@threads[i], @attr, @Hello, @arg[i]) <> 0 then
      Writeln('Failed to create thread');
  end;
  for i := 1 to N do
  begin
    Write('Waiting for thread #',i, ' ... ');
    pthread_join(threads[i], ret);
    Writeln('result: ', res[i]);
  end;
end.