Local variables in C++ and thread safety

Florian Weimer fweimer@redhat.com
Thu Aug 1 12:22:00 GMT 2013

On 08/01/2013 01:01 PM, Александров Петр wrote:

> Are automatic variables (that are defined in functions, lambdas, blocks)
> in C++11 thread local? Is the following code correct:
> auto f1 = [&](const double x) {
>     double y = sin(x);
>     SomeClass1 obj1;
>     double z = obj1.f2(y);
>     return cos(z);
> }
> // Some function which creates several threads which call f1.
> calculate_parallel(f1);

This should be okay, provided that sin, cos are indeed the global 
functions and that SomeClass1 does not touch global state in a 
non-thread-safe way.  You might want to use [] instead of [&], though.

Florian Weimer / Red Hat Product Security Team

More information about the Gcc-help mailing list