Hairy Pixels wrote 7 lines
> On Oct 30, 2022, at 6:55 PM, Marco van de Voort via fpc-pascal wrote:
>
> Yes. They call pthread_detach when ending, i.e. just before calling pthread_exit. So if you use normal threads, you already use that.
>
> Similarly, pthread_join is a part of tthread.waitfor . Have a look at rtl/unix/cthreads.pp for how the RTL threads map to pthreads on *nix systems.
Ok I see that now in cthreads.pp. Do you need to call EndThread from the thread callback when calling BeginThread? I thought the thread cleaned itself up when the function terminated but I may be wrong about that.
I'm not that deep into those procedural threading. From what I remember the prototypes are defined, but not their use.
But since tthread works on top of that, you can simply trace how tthread does it, which is probably more solid and up to date than documentation and two decade old tutorials.
Most notably how tthread.freeonterminate is implemented. Some OSes like Windows (that tthread is modeled after) keep some thread resources around to read the result back after the thread is finished.
Pthreads at least doesn’t appear to require you to detach the thread but maybe it’s leaking memory then.
The manual you quoted seemed to indicate you either have to call _join or _detach.