Yeah, could be a "false positive".
On the other hand, think about this: The function
just a single "atomic" CPU instruction; it is a complex function implemented in libpthread. All we really know is that, at some point in
, the mutex will be marked as "not currently locked", so that the other thread can proceed. But, does
after that point? Is it guaranteed that
won't access the
struct after that point, before returning? I don't think so, because this is an implementation detail that we should not
make any assumptions about. Better assume the "worst" case 😏
In other words, your
function does not
actually has fully returned
inside of the
function; it only waits until the
function has (at least) begun
Surely, any kind of "concurrent" or "interleaved"
invocations on the same
always be safe, assuming that the
was properly initialized. Meanwhile, destorying
be in progress probably results in undefined
I think it's a good guideline to only destroy "shared" resources after all (other) threads that might access those resources have terminated.
EDIT: Is it really necessary to create a new
for each thread? You could simply create those once
and re-use them. This eliminates the need to destroy them (except when your program terminates). Note that your variables are declared
, so you can only have one
at a time anyways. Doesn't make much sense to destroy + re-create them continuously...
EDIT²: If you want to be sure that multiple threads have passed a certain point, consider using a
(But, of course, this creates the new problem of when to call the