Are built-in functions in C / C ++ a way to make them thread safe?

I make the following reasoning, please tell me what is wrong (or right) about it:

"If embedding a function duplicates the code in the place where the function is called, then the static and local variables are duplicated for each calling function, and if there is only one thread executing the function that calls the built-in at one time, then the code is thread safe."

"And if that doesn't help with static and global variables, does the code that creates the temporary variables do this?"

thanks

+6
c ++ c multithreading thread-safety
source share
11 answers

When you declare a function as inline, this is just a hint to the compiler. Static variables have a clear definition in the language. If the compiler performs an inline function, it is still required to maintain static variables shared between all instances of the function. Therefore, they will remain global and must be protected in the MT environment.

As for local variables, if they are not used outside the function, they are thread safe regardless of the built-in function or not.

+25
source share

No, you are mistaken. For a static variable, whether it is built-in or not, there is only one instance. Built-in functions do not affect thread safety, anyway.

+28
source share

Each thread gets its own copy of local objects, so they cannot cause any problems with the threads, regardless of whether you make them inline or not.

But if you get access to a static or member variable of a class, all the problems associated with multi-threaded (variable corruption, lost update ...) will still be present regardless of whether it is built-in or not.

+6
source share

Embedding and thread safety are orthogonal, i.e. unrelated concepts.

Consider the following functions:

  int factorial (const int n)
 {
   if (n <= 1)
   {
     return 1;
   }

   return factorial (n - 1);
 }

This function cannot be built in as it is recursive, but it is perfectly protected by a stream.

  int factorial_2 (int n)
 {
   int Result = 1;

   while (n> 1)
   {
     Result * = n--;
   }

   return Result;
 }

This function can be built into the compiler and is still perfectly protected by the stream.

  int RefCount;

 void DecRef ()
 {
   --RefCount;
 }

This function is not thread safe, regardless of whether its compiler is connected or not.

+3
source share

Previous versions of C ++ (and the current version of C) do not support the concept of threads. This is simply not standard. Therefore, relying on some compiler optimizations to make your code thread safe is not a good idea.

+3
source share

There are (or possibly were) error compilers that duplicate static variables for each insertion of the contained function. This is not an intentional behavior.

When using the standard compiler, you are guaranteed to have one instance of each static variable, regardless of the attachment, and you must take care of thread safety yourself.

+2
source share

Thread safety has nothing to do with embedding. Just because each thread executes a copy of the same code does not make it safe to access shared data. Be sure to read Thread Safety before starting multithreaded programming.

+2
source share

If I were you, I would not use static variables inside inline functions. Older versions of the C ++ standard called a different value than the current one.

+2
source share

inlining does not affect whether the function is actually thread safe. For example:

inline void doTheThing() { int a = 42; // this is thread safe, but it would be anyway vector<int> * answers = getTheAnswers(); // this is not thread safe } 

Access to the vector pointed to by getTheAnswers () is not thread safe because there is no code that prevents code from being executed by another thread. Executing the inline function does not prevent doTheThing () from being called by multiple threads at the same time. To make the doTheThing () stream safe, you need to make sure that it is either not called in parallel or that all available (non-local) data that you make is protected.

+1
source share

Not at all.

To be thread safe, a function must be thread safe with respect to any instance of the function, including itself; multiple threads can simultaneously execute the same instance of an inline function.

Therefore, even if the compiler did what you suggested (which should not, as others said), it still will not make it thread safe.

0
source share

All static variables in (normal and built-in) functions go to heap. Heap is NOT THREAD SAFE .

-2
source share

All Articles