简体   繁体   English

c ++多线程参数问题

[英]c++ Multi-threading Argument Issues

I'm trying to pass a series of parameters into different c++ threads. 我试图将一系列参数传递给不同的c ++线程。 The program runs fine when NumThreads == 1, however when NumThreads > 1, the p parameter I pass to the function is incorrect within the thread. 当NumThreads == 1时程序运行正常,但是当NumThreads> 1时,我传递给函数的p参数在线程内不正确。 Am I missing something in the thread constructor and not passing p by value? 我是否在线程构造函数中缺少某些内容,并且未按值传递p?

Where the threads are created: 创建线程的位置:

int NumThreads = 2;
std::thread t[numSamplePoints];
std::mutex dataLock;

for( int i = 0 ; i < numSamplePoints ; i++)
{
  // Prevent more than NumThreads from running at once
  if( i > NumThreads && t[i-NumThreads].joinable() )
  {
    t[i - this->NumThreads].join();
  }

  // Set and Check Input Parameters
  double p[3];
  srcPoints->GetPoint(i , p);
  if( i < 3 )
  {
    cout<< "OUTTHREAD " << p[0] << " " << p[1] << " " << p[2] <<endl;
    cout<< "src: " << Id << " index: " << i <<endl;
  }

  t[i] = std::thread(&MyClass::MyFunction, this, &dataLock, i, Id, p);
}

And the member function being called: 成员函数被调用:

void MyClass::MyFunction(std::mutex *dataLock, int sampleIndex, int Id, double srcPoint[3])
{
  dataLock->lock();
  if( sampleIndex < 3)
  {
    cout<< "IN THREAD " << srcPoint[0] << " " << srcPoint[1] << " " << srcPoint[2] <<endl;
    cout<< "src: " << sourceId << " index: " << sampleIndex <<endl;
  }
  dataLock->unlock();
}

the console output from the first three threads: { 前三个线程的控制台输出:{

OUTTHREAD 45.7694 1.06209 -60.9628
src: 0 index: 0
OUTTHREAD 48.6044 -5.40514 -54.7663
src: 108 index: 1
OUTTHREAD 52.505 9.00298 -47.0499
src: 216 index: 2

IN THREAD 52.505 9.00298 -47.0499
src: 0 index: 0
IN THREAD 52.505 9.00298 -47.0499
src: 108 index: 1
IN THREAD 52.505 9.00298 -47.0499
src: 216 index: 2

So ID and sample index are being passed correctly to the threads, but how is srcPoint the same for all three threads?!? 因此ID和样本索引已正确传递到线程,但是srcPoint对于所有三个线程如何相同?

You are invoking undefined behavior by passing a pointer to a local variable to your threads and allowing the variable to go out of scope before it's used. 您正在通过将指向局部变量的指针传递给线程并允许变量在使用之前超出范围来调用未定义的行为。

C-style arrays are never passed by value. C样式的数组永远不会按值传递。 A function declared to take an array type as an argument actually takes a pointer: 声明为采用数组类型作为参数的函数实际上需要一个指针:

void MyClass::MyFunction(std::mutex *dataLock, int sampleIndex, int Id, double srcPoint[3])

is equivalent to 相当于

void MyClass::MyFunction(std::mutex *dataLock, int sampleIndex, int Id, double* srcPoint)

In this case, your p array is local to your for loop scope, and it implicitly decays into a pointer to its first element when passed to your thread constructor. 在这种情况下,您的p数组在for循环作用域内是局部的,并且在传递给thread构造函数时,它隐式衰减为指向其第一个元素的指针。 As soon as each iteration of your loop completes, p goes out of scope and is destroyed, but your thread still has a pointer to the memory it used to inhabit. 一旦循环的每次迭代完成, p就会超出范围并被销毁,但是您的线程仍具有指向它曾经驻留的内存的指针。

The best option to fix this would be to replace double p[3] with std::array<double, 3> p in your loop and to make MyClass::MyFunction take a parameter std::array<double, 3> srcPoint instead of double srcPoint[3] . 解决此问题的最佳方法是在循环中将double p[3]替换为std::array<double, 3> p并使MyClass::MyFunction接受参数std::array<double, 3> srcPoint代替double srcPoint[3] Unlike raw C-style arrays, std::array can be passed by value, and implements the copy semantics you expect. 与原始C样式数组不同, std::array可以按值传递,并实现您期望的复制语义。

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

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