The pthread_exit function should take a void pointer as input. I'm wondering how come it's possible to pass the address of an integer variable (e.g. pthread_exit(&ret1) here) without performing a cast conversion.
Asked
Active
Viewed 73 times
0
-
Might be an implicit conversion from any pointer type to a void pointer. I'd have to consult the C standard if it has anything about that. – Christian Gibbons Jul 02 '20 at 17:56
-
3Any object type pointer can be implicitly and silently converted to/from `void*`. Here is the relevant standard section: http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p1 – Eugene Sh. Jul 02 '20 at 18:10
-
@EugeneSh. I haven't been able to find anything in that section of the standard about implicit casting. – Christian Gibbons Jul 02 '20 at 19:10
-
2@ChristianGibbons There is rather the rules for what requires the explicit casting: http://port70.net/~nsz/c/c11/n1570.html#6.5.4p3 Hard to read stuff though. – Eugene Sh. Jul 02 '20 at 19:17
-
2@ChristianGibbons [6.5.16.1/1](http://port70.net/~nsz/c/c11/n1570.html#6.5.16.1p1): "the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void" – P.P Jul 02 '20 at 19:22
-
Ah, I see, it points to 6.5.16.1 which is about assignment, and if the assignment is okay as-is, then there would be no need to cast. – Christian Gibbons Jul 02 '20 at 19:23
-
Hello @EugeneSh. could you post the sentence please...i have some difficult to find it – Gennaro Arguzzi Jul 02 '20 at 19:33
-
BTW, the article you pointed to is not a good exemplar. Passing the address of a local variable to `pthread_exit` is a really horrible practice. A local variable is located on the stack of the exiting thread, and the state of that stack following the completion of `pthread_exit` is undefined (and in particular it could have been deallocated or re-used by the time the address is returned to the `pthread_join` caller). See the 4th paragraph of the description here: https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_exit.html – Gil Hamilton Jul 03 '20 at 22:21
-
@GilHamilton ret1 is a global variable – Gennaro Arguzzi Jul 04 '20 at 06:30
-
Ah. Thanks, @GennaroArguzzi. I stand corrected. I do think it's still a dumb way to return the thread's exit status, though: better to just cast the exit status directly to `void *`. – Gil Hamilton Jul 05 '20 at 21:14