On Thu, 27 Aug 2015 11:12:28 -0700, Prroffessorr Fir Kenobi wrote:
> Im not sure if this is a problem of c++ only or it appleies to c, too
>
> Sometimes (usually?) when using static entities in functions the compiler
> puts special code which contains a lock acquire and release
>
> Thisd bloats the executable yet also slows down the code.. Im not quite
> sure
> 1) how serious this problem is?
How frequently are those functions called? And if they're called from
multiple threads, how much contention do you expect for the locks?
If you don't need the functions to be re-entrant (i.e. you're not using
threads or such functions are never called from more than one thread),
with g++ you can use -fno-threadsafe-statics to remove the locks.
> 2) in which cases it do apply?
It occurs whenever you have a static local variable whose constructor
requires the execution of code (i.e. the compiler can't just store the
initial value in the data/bss segment, but must calculate it at run time),
and the compiler can't somehow optimise the locks away.
> 3) how to avoid it in most easy way?
> 4) how to be sure my app do not contains such sh*t?
Don't use static local variables. Put the variables at file scope so that
they're guaranteed to be initialised before main() is entered.
If you can't do that (e.g. because the initial value depends upon data
which hasn't been initialised at that point), then the construction has to
occur on the first call to the function, and the compiler has to allow for
the possibility that multiple threads call the function concurrently
(unless it has been told that this cannot happen; it's not as if it can
deduce that by itself).