virThread: why no return values permitted in thread API?

Hi, I noticed that in the virThread API return values from threads are explicitly excluded from the API. The "virThreadHelper" sits in the middle between the caller of virThreadCreate and pthread, making it impossible to return a value from the thread, to check after joining the thread. I'd need to know if the thread execution is successful or not then I join the thread, so what is the reason for this choice? Should I use pthread directly instead? Thanks, Claudio -- Claudio Fontana Engineering Manager Virtualization, SUSE Labs Core SUSE Software Solutions Italy Srl

On Wed, Apr 20, 2022 at 14:24:39 +0200, Claudio Fontana wrote:
Hi,
I noticed that in the virThread API return values from threads are explicitly excluded from the API.
The "virThreadHelper" sits in the middle between the caller of virThreadCreate and pthread, making it impossible to return a value from the thread, to check after joining the thread.
virThreadCreate has an 'opaque' parameter which allows to pass arbitrary data to the trhead. You can use it to pass also data back, just apply thread safety rules.
I'd need to know if the thread execution is successful or not then I join the thread, so what is the reason for this choice?
Could you elaborate what you are trying to achieve?
Should I use pthread directly instead?
Generally no, please use the internal helpers for code consistency.

On Wed, Apr 20, 2022 at 02:33:19PM +0200, Peter Krempa wrote:
On Wed, Apr 20, 2022 at 14:24:39 +0200, Claudio Fontana wrote:
Hi,
I noticed that in the virThread API return values from threads are explicitly excluded from the API.
The "virThreadHelper" sits in the middle between the caller of virThreadCreate and pthread, making it impossible to return a value from the thread, to check after joining the thread.
virThreadCreate has an 'opaque' parameter which allows to pass arbitrary data to the trhead. You can use it to pass also data back, just apply thread safety rules.
I'd need to know if the thread execution is successful or not then I join the thread, so what is the reason for this choice?
Could you elaborate what you are trying to achieve?
Should I use pthread directly instead?
Generally no, please use the internal helpers for code consistency.
Though we should really be phasing out much of virthread.h in favour of just using GLib's portable thread APIs. So personally I'd be fine with patches that use GThread in new code. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Apr 20, 2022 at 02:24:39PM +0200, Claudio Fontana wrote:
Hi,
I noticed that in the virThread API return values from threads are explicitly excluded from the API.
The "virThreadHelper" sits in the middle between the caller of virThreadCreate and pthread, making it impossible to return a value from the thread, to check after joining the thread.
Mostly a historical artifact from when we had a native impl of threads for Win32. We didn't need that ability, so I never bothered to try to figure it out for Win32 and thus for consistency I left it out of POSIX impl too. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On 4/20/22 14:24, Claudio Fontana wrote:
Hi,
I noticed that in the virThread API return values from threads are explicitly excluded from the API.
The "virThreadHelper" sits in the middle between the caller of virThreadCreate and pthread, making it impossible to return a value from the thread, to check after joining the thread.
I'd need to know if the thread execution is successful or not then I join the thread, so what is the reason for this choice?
Should I use pthread directly instead?
Not at all. Just change the type of virThreadJoin(). At which point it's harder to distinguish whether thread ran and returned a value, or thread did not run/exited before the callback was called and retval we are seeing comes from virThreadHelper(), but since it does not return anything yet, it's easy to distinguish the two. Whatever you do, please avoid using pthread directly. Michal
participants (4)
-
Claudio Fontana
-
Daniel P. Berrangé
-
Michal Prívozník
-
Peter Krempa