简体   繁体   English

C ++局部变量和线程(不是thread_local)

[英]C++ local variables and threads (not thread_local)

What are the C++98 and C++11 memory models for local arrays and interactions with threads? 用于本地数组和与线程交互的C ++ 98和C ++ 11内存模型是什么?

I'm not referring to the C++11 thread_local keyword, which pertains to global and static variables. 不是指C ++ 11 thread_local关键字,它与全局变量和静态变量有关。

Instead, I'd like to find out what is the guaranteed behavior of threads for arrays that are allocated at compile-time. 相反,我想知道在编译时分配的数组的线程的保证行为是什么。 By compile-time I mean "int array[100]", which is different to allocation using the new[] keyword. 编译时我的意思是“int array [100]”,这与使用new []关键字的分配不同。 I do not mean static variables. 我不是指静态变量。

For example, let's say I have the following struct/class: 例如,假设我有以下结构/类:

struct xyz { int array[100]; };

and the following function: 以及以下功能:

void fn(int x) {
  xyz dog;
  for(int i=0; i<100; ++i)  { dog.array[i] = x; }
  // do something else with dog.array, eg. call another function with dog as parameter
  }

Is it safe to call fn() from multiple threads? 从多个线程调用fn()是否安全? It seems that the C++ memory model is: all local non-static variables and arrays are allocated on the stack, and that each thread has its own stack. 似乎C ++内存模型是:所有本地非静态变量和数组都在堆栈上分配,并且每个线程都有自己的堆栈。 Is this true (ie. is this officially part of the standard) ? 这是真的吗(即,这是标准的正式部分)吗?

Such variables are allocated on the stack, and since each thread has its own stack, it is perfectly safe to use local arrays. 这些变量在堆栈上分配,并且由于每个线程都有自己的堆栈,因此使用本地数组是完全安全的。 They are not different from eg local int s. 它们与例如local int s没有区别。

C++98 didn't say anything about threads. C ++ 98对线程没有任何说明。 Programs otherwise written to C++98 but which use threads do not have a meaning that is defined by C++98. 以其他方式写入C ++ 98但使用线程的程序没有C ++ 98定义的含义。 Of course, it's sensible for threading extensions to provide stable, private local variables to threads, and they usually do. 当然,线程扩展为线程提供稳定的私有局部变量是明智的,他们通常会这样做。 But there can exist threads for which this is not the case: for instance processes created by vfork on some Unix systems, whereby the parent and child will execute in the same stack frame, since the v in vfork means not to clone the address space, and vfork does not redirect the new process to a different function, either. 但是可能存在这样的情况:例如vfork在某些Unix系统上创建的进程,父进程和子进程将在同一堆栈帧中执行,因为vforkv意味着不克隆地址空间,并且vfork也不会将新进程重定向到其他功能。

In C++11, there is threading support. 在C ++ 11中,有线程支持。 Local variables in separate activation chains in separate C++11 threads do not interfere. 单独的C ++ 11线程中的单独激活链中的局部变量不会产生干扰。 But if you go outside the language and whip out vfork or anything resembling it, then all bets are off, like before. 但是,如果你超出语言并vfork或任何类似的东西,那么所有的赌注都会像以前一样关闭。

But here is something. 但这里有一些东西。 C++ has closures now. C ++现在已经关闭了。 What if two threads both invoke the same closure? 如果两个线程都调用相同的闭包怎么办? Then you have two threads sharing the same local variables. 然后你有两个线程共享相同的局部变量。 A closure is like an object and its captured local variables are like members. 闭包就像一个对象,它捕获的局部变量就像成员一样。 If two or more threads call the same closure, then you have a de facto multi-threaded object whose members (ie captured lexical variables) are shared. 如果两个或多个线程调用相同的闭包,那么你有一个事实上的多线程对象,其成员(即捕获的词法变量)是共享的。

A thread can also simply pass the address of its local variables to another thread, thereby causing them to become shared. 线程也可以简单地将其局部变量的地址传递给另一个线程,从而使它们变得共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM