This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Local variables in C++ and thread safety
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Александров Петр <alexandrov at dio dot ru>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Thu, 1 Aug 2013 13:10:40 +0100
- Subject: Re: Local variables in C++ and thread safety
- References: <51FA4023 dot 90503 at dio dot ru>
On 1 August 2013 12:01, Александров Петр wrote:
> Hello.
> 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);
"thread local" has a specific meaning and refers to objects declared
thread_local, which have "thread storage duration", which means
storage for the variables lasts as long as the thread is running.
Automatic variables have "automatic storage duration" instead, which
means they are on the stack of the calling thread and their storage
only lasts until exit from the block in which they are created.
If you're asking whether each thread has its own copy of the variable,
yes, of course.