C++ 使用std::thread变量数的引用出现问题
我已经使用过posix线程(pthread),但我正在尝试使用标准的C++11线程(很可能下面也是pthread)。我在这里和其他论坛上发了好几篇帖子,但我无法解决我的问题。如果有人能给我点灯,我会很高兴的 问题是,我试图将数据传递给线程,线程修改内部数据,并且在抛出的线程完成(连接)后,主线程必须看到修改。问题是,在某些线程中,数据(int)读取不正确,因此输出结果不正确 我可以编写的复制问题的最低代码是:C++ 使用std::thread变量数的引用出现问题,c++,multithreading,c++11,stdthread,C++,Multithreading,C++11,Stdthread,我已经使用过posix线程(pthread),但我正在尝试使用标准的C++11线程(很可能下面也是pthread)。我在这里和其他论坛上发了好几篇帖子,但我无法解决我的问题。如果有人能给我点灯,我会很高兴的 问题是,我试图将数据传递给线程,线程修改内部数据,并且在抛出的线程完成(连接)后,主线程必须看到修改。问题是,在某些线程中,数据(int)读取不正确,因此输出结果不正确 我可以编写的复制问题的最低代码是: #include <iostream> #include <vect
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void threadCallbackInt(int const & x)
{
int & y = const_cast<int &>(x);
y += 10;
std::cout<<"Inside Thread x = "<<y << " at position = " << &y <<std::endl;
}
int main(int argc, char* argv[])
{
unsigned int nthreads = 3;
vector<int> ints;
vector<std::thread> threads(nthreads);
threads.reserve(nthreads);
for(unsigned int i = 0; i < nthreads; i++){
ints.push_back(i);
std::cout<<"In Main Thread : Before Thread Start x = "<<ints.at(i)<< " at position = " << &ints.at(i) << std::endl;
threads[i] = std::thread(threadCallbackInt,std::ref(ints[i]));
}
cout << "size = " << threads.size() << endl;
for(unsigned int i = 0; i < nthreads; i++){
threads[i].join();
}
for(unsigned int i = 0; i < nthreads; i++){
std::cout<<"In Main Thread : After Thread Start x = "<<ints[i]<< " at position = " << &ints[i] << std::endl;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
void threadCallbackInt(int const&x)
{
int&y=常数(x);
y+=10;
std::cout当ints
调整ints
的大小时,引用std::ref(ints[i])
无效。向后推(i);
。如果您希望这样做,还需要在ints
上使用reserve
在使用gcc进行测试时
- 当
时,则i==0
ints.capacity()==1
- 当
时,则i==1
ints.capacity()==2
- 当
时,则i==2
ints.capacity()==4
这表明确实发生了调整大小。引用
std::ref(ints[i])
在ints
调整ints的大小时无效。推回(i);
。如果您希望这样做,您还需要在ints
上使用reserve
在使用gcc进行测试时
- 当
时,则i==0
ints.capacity()==1
- 当
时,则i==1
ints.capacity()==2
- 当
时,则i==2
ints.capacity()==4
这表明确实发生了调整大小的操作。在执行ints.push_back(i)后[0]的地址正在更改。
通过添加
std::cout,在执行int.push_back(i)后[0]的地址正在更改。
通过添加std::cout“由*表示的线程的完成与(1.10)相应的成功join()
返回同步”该标准保证线程的所有更改在时间>代码>连接< /代码>返回时都是可见的。谢谢——我非常谨慎地访问C++中的非互斥的非核的其他线程修改数据,因为它是如何容易地生成UB的。多谢FalCon!现在它工作得很好!我把重点放在线程和P的向量上。正在为该向量设置reserve(),但我忘记了向量int。非常感谢。“由*表示的线程的完成与(1.10)相应的成功连接()
返回同步”该标准保证线程的所有更改在时间>代码>连接< /代码>返回时都是可见的。谢谢——我非常谨慎地访问C++中的非互斥的非核的其他线程修改数据,因为它是如何容易地生成UB的。多谢FalCon!现在它工作得很好!我把重点放在线程和P的向量上。正在为那个向量设置reserve(),但是我忘记了向量int。非常感谢。非常感谢!!!现在它工作了!你知道,我一直在为线程的向量设置reserve(),但是我忘记了int的向量。非常感谢。非常感谢!!!现在它工作了!你知道,我一直在关注reserve()对于线程的向量,但是我忘记了int的向量。非常感谢。
ints.size()=1 &ints[0]=0095BA28
In Main Thread : Before Thread Start x = 0 at position = 0095BA28
Inside Thread x = 10 at position = 0095BA28
ints.size()=2 &ints[0]=0095BA38
In Main Thread : Before Thread Start x = 1 at position = 0095BA3C
Inside Thread x = 11 at position = 0095BA3C
ints.size()=3 &ints[0]=0095E940
In Main Thread : Before Thread Start x = 2 at position = 0095E948
Inside Thread x = 12 at position = 0095E948
size = 3
In Main Thread : After Thread Start x = 10 at position = 0095E940
In Main Thread : After Thread Start x = 11 at position = 0095E944
In Main Thread : After Thread Start x = 12 at position = 0095E948
ints.size()=1 &ints[0]=00DD8608
In Main Thread : Before Thread Start x = 0 at position = 00DD8608
Inside Thread x = 10 at position = 00DD8608
ints.size()=2 &ints[0]=00DD8608
In Main Thread : Before Thread Start x = 1 at position = 00DD860C
Inside Thread x = 11 at position = 00DD860C
ints.size()=3 &ints[0]=00DD8608
In Main Thread : Before Thread Start x = 2 at position = 00DD8610
Inside Thread x = 12 at position = 00DD8610
size = 3
In Main Thread : After Thread Start x = 10 at position = 00DD8608
In Main Thread : After Thread Start x = 11 at position = 00DD860C
In Main Thread : After Thread Start x = 12 at position = 00DD8610