tw14265.pp 917 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. { %target=linux }
  2. { %opt=-Xt }
  3. program phello;
  4. {$linklib c}
  5. {$packrecords c}
  6. uses
  7. ctypes, unixtype, pthreads;
  8. const N = 2;
  9. var
  10. res:array[1..N] of Integer;
  11. function Hello(arg: pointer): longint; cdecl;
  12. begin
  13. // writeln('Hello from thread #', PInteger(arg)^);
  14. res[PInteger(arg)^] := PInteger(arg)^;
  15. Hello := 0;
  16. pthread_exit(pointer(Hello));
  17. end;
  18. var
  19. i: Integer;
  20. ret: Pointer;
  21. arg: array[1..N] of Integer;
  22. threads: array[1..N] of TThreadID;
  23. attr: TThreadAttr;
  24. begin
  25. Writeln('Testing simple thread creation');
  26. pthread_attr_init(attr);
  27. for i := 1 to N do
  28. begin
  29. Writeln('Creating thread #',i);
  30. arg[i] := i;
  31. if pthread_create(threads[i], attr, @Hello, @arg[i]) <> 0 then
  32. Writeln('Failed to create thread');
  33. end;
  34. for i := 1 to N do
  35. begin
  36. Write('Waiting for thread #',i, ' ... ');
  37. pthread_join(threads[i], ret);
  38. Writeln('result: ', res[i]);
  39. end;
  40. end.