top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Local variables in C++ and thread safety

+1 vote

Are automatic variables (that are defined in functions, lambdas, blocks) in C++11 thread local? Is the following code correct:

auto f1 = [
 SomeClass1 obj1;
 double z = obj1.f2(y);
 return cos(z);

// Some function which creates several threads which call f1.

posted Aug 1, 2013 by Sanketi Garg

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

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.

answer Aug 1, 2013 by Deepak Dasgupta
0 votes

"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.

answer Aug 1, 2013 by Deepankar Dubey
Similar Questions
+1 vote

I'm going to use SSLRequire using the worker MPM.

The docs say, "The implementation of SSLRequire is not thread safe. Using SSLRequire inside .htaccess files on a threaded MPM may cause random crashes."

Does this mean that I'm okay with threaded MPM if I'm only using SSLRequire inside httpd.conf and not in .htaccess?

0 votes

how can I configure GCC to emit the calls to __cxa_thread_atexit() for destructor registration? On a Linux PowerPC target I see the registration of destructors, but not on the powerpc-rtems target. I guess I missed a configuration option or define. Has someone a hint for me?