looking for some solutions? You are welcome.

SOLVED: C++: Proper way to share value (data) between different threads

Jerwin Prabu:

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) 
            {

               Thread::sync_quality=nodes[pos].sync_quality & RPLIDAR_RESP_MEASUREMENT_SYNCBIT;
               Thread::angle_q6_checkbit=(nodes[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT)/64.0f;
               Thread::distance_q2=nodes[pos].distance_q2/4.0f;
               Thread::sync_qualitylevel=nodes[pos].sync_quality >> RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT;

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

       if(this->Stop)
        {
           drv->stop();
           drv->stopMotor();
           RPlidarDriver::DisposeDriver(drv);
           drv = NULL;
           break;
        }
    }
  return ;
}

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.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent