top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Local variables in C++ and thread safety

+1 vote
53 views

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.
calculate_parallel(f1);

posted Aug 1, 2013 by Sanketi Garg

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social 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?

0 votes

I heard the term "worker thread" and other types of threads such as "system thread" and "user thread".
For me a thread is nothing but a lite weight process and multi-threaded design is chosen for a software development when there are multiple task in the application/system and those tasks can be executed independently.
I want to know when these different terms are used like worker thread, user thread and system thread ?


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...