Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2022-10

pthread_detach

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marco van de Voort
to Hairy Pixels
Hairy Pixels wrote
The POSIX thread library has a function called pthread_detach (https://man7.org/linux/man-pages/man3/pthread_detach.3.html). Do the RTL threads have anything comparable?

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.

Honestly I don’t really understand why this is even needed because the threads seem to clean themselves up when they exit anyways but since it exists in pthreads I thought I was ask.

Maybe there are other uses of pthread_detach that don't immediately call _exit.  I'm not pthreads expert, so I can't help there.

Reply by email

Hairy Pixels
to Marco van de Voort
Show quoted text 5 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. Pthreads at least doesn’t appear to require you to detach the thread but maybe it’s leaking memory then.

Regards,
Ryan Joseph

Reply by email

Marco van de Voort
to Hairy Pixels
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.

Reply by email

Hairy Pixels
to Marco van de Voort
Show quoted text 11 lines
On Oct 30, 2022, at 7:20 PM, Marco van de Voort via fpc-pascal wrote: 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.

Yes you’re right, I didn’t read all the way to the bottom. It says you must call join OR detach to free resources. That doesn’t sound right you could only call join but who knows..

I tried to call EndThread (which calls pthread_detach) after the function reaches the end but I get a crash. Looking BeginThread it appears to call the user callback in a wrapper and does cleanup for you but I’m not 100% clear about this. Docs (https://www.freepascal.org/docs-html/rtl/system/beginthread.html) don’t say you must cleanup either so I’m guessing you don’t have to.

Regards,
Ryan Joseph

Reply by email