c - Function using a local static variable thread safe/reentrant -
i have function generate unique random number(basically increment previous) different threads invoking it.
is thread safe or reentrant.assume used static variable number.
i have seen in forum static variables cant used reentrant/thread safe.
does applies local/global static.
or implementation defined.
changing "ordinary" object shared between threads never thread safe, unless take special care of it. (and statically declared variable falls in category). there 2 standard ways of dealing that
- use mutex or other lock structure protect shared object inside "critical section"
- use atomic operations access object, new c standard, c11 has interfaces this
being reentrant asks if execution (even without threads) can modify part of state, examples recursion, signal handlers or jumps goto
or longjmp
. think of sharing variable "yourself". statically allocated variables make same problems here, if modify them different places of program.
Comments
Post a Comment