Quantcast
Channel: c++ Multi-threading Argument Issues - Stack Overflow
Viewing all articles
Browse latest Browse all 2

c++ Multi-threading Argument Issues

$
0
0

I'm trying to pass a series of parameters into different c++ threads. The program runs fine when NumThreads == 1, however when NumThreads > 1, the p parameter I pass to the function is incorrect within the thread. Am I missing something in the thread constructor and not passing p by value?

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.9628src: 0 index: 0OUTTHREAD 48.6044 -5.40514 -54.7663src: 108 index: 1OUTTHREAD 52.505 9.00298 -47.0499src: 216 index: 2IN THREAD 52.505 9.00298 -47.0499src: 0 index: 0IN THREAD 52.505 9.00298 -47.0499src: 108 index: 1IN THREAD 52.505 9.00298 -47.0499src: 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?!?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images