|
Boost : |
From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-08-10 04:20:43
> Q. Is it safe for threads to share automatic storage duration (stack)
> objects via pointers or references?
>
> A. Only if you can guarantee lifetime of the stack object will not end
> while other threads might still access the object. Thus the safest
> practice is to avoid sharing stack objects, particularly when in designs
> where threads are created and destroyed dynamically. Restrict sharing of
> stack objects to simple designs with very clear and unchanging function
and
> thread lifetimes.
>
> Comments?
whether it is automatic storage duration or something else
programmer should provide synchronization for two things:
a) r/w access; writes/updates should preclude concurrent reads.
(always! even for static volatile sig_atomic_t variables!
non-portable things such as memory mapped hardware registers
aside)
b) object destruction; an object should never be destroyed while
some other thread(s) may still be using it/some other reachable
refs/ptrs to it are still hanging somewhere around.
regards,
alexander.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk