0

I have the following Thread.cpp:

   while (1) 
    {
       rplidar_response_measurement_node_t nodes[8192];
       size_t   count = _countof(nodes);

       op_result = drv->grabScanData(nodes, count);

       if (IS_OK(op_result)) 
        {
           drv->ascendScanData(nodes, count);

           for (int pos = 0; pos < (int)count ; ++pos) 
            {

               nodes[pos].sync_quality & RPLIDAR_RESP_MEASUREMENT_SYNCBIT;
               nodes[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT/64.0f;
               nodes[pos].distance_q2/4.0f;
               nodes[pos].sync_quality >> RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT;
            }
        }
    }
  return ;
}

Motion Thread.cpp

void Motion::Synchronization(QString motiontype)
{
      if(motiontype == START)
       {

   if(269<Thread::angle_q6_checkbit && Thread::angle_q6_checkbit <271)
    {
        if(Thread::distance_q2>1200)
        {
            dist_left=1200;
        }
     else
      {
           dist_left=Thread::distance_q2;
      }
                cout<<"Motion +Stuff+="<< endl;
    }
  }
}

This isn't my exact code, this is the part where I will get the data and storing data into the variable. distance_q2, angle_q6_checkbit, etc. are NOT an atomic variable, so reading and writing them from a different thread is not consistent!! In the same thread, if I tried to print data I am getting the proper reading. The output is given below.

Proper data from Thread.cpp

Now, I have tried to take the data for processing or other my stuff. Storing the distance, angle quality data like to Thread::sync_quality, Thread::angle_q6_checkbit, Thread::distance

Taking data from Laser data Thread.cpp to motion.cpp thread (cout data in motion thread):

cout << "distance "<< Thread::distance_q2 << endl;
cout << "angle "<< Thread::angle_q6_checkbit << endl;
cout << "quality level "<< int(Thread::sync_qualitylevel) << endl;

I am getting improper output (getting only 359 degrees, that also improper). I can't able to conclude. Now the output for the above is:

Improper data with another thread motion.coo

I've tried to declare then as atomic, but I can't see any improvement. Help would be greatly appreciated.

Jerwin Prabu
  • 336
  • 1
  • 3
  • 16
  • Perhaps the problem isn't with the sharing, but in your calculations? How much of the data is shared? Is it "true" sharing, where each thread access the exact same data? Or is each thread using different parts of the data (for example thread one uses array elements `0` to `9`, thread two uses array elements `10` to `19`, etc.)? Does it work if you run only a single thread? Or call the function directly without using threads at all? – Some programmer dude Mar 15 '19 at 07:09
  • And have you tried to [debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? It's not easy with multi-threaded programs, but if you step through *one* thread in a debugger, does it do what you expect it to do? You could also add some debug-logging or output to help figure out where the problem might be. – Some programmer dude Mar 15 '19 at 07:12
  • If the problem within my calculation, then same thread only I will not get proper data, but I am getting proper data continuously ( theta: 0.11 Dist: 00000.00 Q: 0 theta: 0.73 Dist: 00000.00 Q: 0 theta: 1.38 Dist: 00000.00 Q: 0 theta: 3.30 Dist: 00000.00 Q: 0 to theta: 358.84 Dist: 08949.00 Q: 47 theta: 359.48 Dist: 08986.00 Q: 47 ). Yes, it is true and motion thread access the exact same data. Once if I am getting same data then only I can take different parts. Thread1,2,3,4,5 started and stopped properly. If I want to share data from this thread, that is not happening. – Jerwin Prabu Mar 15 '19 at 07:22
  • It is not happening means, I am not getting proper data. – Jerwin Prabu Mar 15 '19 at 07:23
  • I can't see any kind of inter thread communication here. Simply read/write on the same variables will ever fail! mutex? atomic? shared memory? pipes? What is the question about? – Klaus Mar 15 '19 at 08:27
  • Storing the distance, angle, quality data to Thread::sync_quality, Thread::angle_q6_checkbit, Thread::distance. When I am try to use this data to other thread I am not getting the full data I am getting only last data theta: 359.48 Dist: 08986.00 Q: 47. mutex? atomic? shared memory? pipes? I am not sure, What can I use to get the proper data. I have updated the part of thread communication. – Jerwin Prabu Mar 15 '19 at 09:55
  • I think problem because of for loop. I am getting the output data like this. // from Thread.cpp theta: 357.36 Dist: 09147.00 Q: 47 theta: 358.00 Dist: 08823.00 Q: 47 theta: 358.64 Dist: 08964.00 Q: 47 theta: 359.92 Dist: 09140.00 Q: 47 distance 9140 // from motionthread.cpp angle 359 quality level 47 distance 9140 angle 359 quality level 47 distance 9140 angle 359 quality level 47 theta: 0.12 Dist: 00000.00 Q: 0 // from Thread.cpp theta: 0.77 Dist: 00000.00 Q: 0 theta: 1.42 Dist: 00000.00 Q: 0 after completed for loop i am getting last data – Jerwin Prabu Mar 15 '19 at 13:49
  • So, if this is a problem, what can be the solutions. – Jerwin Prabu Mar 15 '19 at 13:57

0 Answers0