Question

I've been researching how synchronization works in Boost, but I'm still somewhat confused about what I need to solve my problem. I have two threads, the main one for my Augmented Reality component of the application and a second thread for workplace calibration that acquires the needed positions.

The 2nd thread is initiated currently only if a calibration is required and will be the only one writing to the vector hdData displayed below:

#include <boost/thread.hpp>

/* Holds data retrieved from HDAPI. */
struct DeviceData {
    hduVector3Dd m_devicePosition; // Current device coordinates
    hduMatrix m_transform; // Current device transform matrix
};

class HapticDevice {
public:
    HapticDevice();

    std::vector<DeviceData> hdData;

    HDSchedulerHandle hPositionCycle;
    HHD hHD;

    bool thread_running;
    boost::thread haptic_thread;

    void startThread();
    void threadPosAcquisition();

    void haptic_cleanup(HDSchedulerHandle hPositionCycle, HHD hHD);
}; 

And the thread is initialized inside the HapticDevice class when position acquisition is required:

HDCallbackCode HDCALLBACK positionAcquisitionCallback(void *pUserData);

HapticDevice::HapticDevice() {
thread_running = false;
}

void HapticDevice::startThread() {
thread_running = true;
haptic_thread = boost::thread(boost::bind(&HapticDevice::threadPosAcquisition, this));
}

void HapticDevice::threadPosAcquisition() {
    HapticDevice *hdev = (HapticDevice *) pUserData;
    //acquires position per click made by user
    //...
}

The writing is controlled by the user (per click a position is acquired and pushed into the vector), but the reading is controlled by the main thread. I want to be able to share the vector between the two threads, but I haven't quite figured out how. Where would I place a mutex?

I found a few questions here that were similar and passed the class through a boost::ref, but since I'm using a method of the class to start the thread, it only confused me further if I need to change my code or not...

If I simply define the haptic device object in my main.cpp like so

HapticDevice haptic_device;

And start the thread, how do I access the changes that the 2nd thread is making? I'm not quite sure how much sense I currently make, my apologies.

Was it helpful?

Solution

Well, the synchromization is quite a large topic itself. Start with the boost documentation.

In simpliest case, when you have one reader and one writer you may use boost::mutex object and boost::unique_lock guard object inside your reader and write functions.

class HapticDevice {
...
protected:
    boost::muitex _mutex;
....
}

Somewhere in reader or writer function:

boost::unique_lock<boost::mutex> _guard(hapticDeviceObject->_mutex);

Which is in generally a bad code style. Depending on your algorithm, you rather create functions to add data, something like HapticDevice::addDeviceData(const &DeviceData), and retrieve data, like HapticDevice::getDeviceData(), and do all the required locking inside them.

The unique_lock guard object is needed to release mutex when executions goes out of reader or writer functions (like an exception or just end of the function).

If you have concurent readers you may consider using shared_lock for them to increase performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top