ottawaDan
3rd October 2012, 16:53
Good morning,
I am doing some refactoring and I came across the following issue. I am running a multi-thread program, where a pair of threads (call the writingThread and readingThread) are accessing a shared object. One thread is processing an image and sets a data member in the shared object when the desired condition occurs while the second thread (readingThread) waits for the given condition (data member set to continue its process).
The shared object has the following declaration:
class TargetTracking : public QObject
{
...
public:
bool isTargetAligned() { return m_targetIsAligned; }
void overrideTargetAlignment( const bool action) { m_overrideTargetAlignment = action; }
void waitForAlign();
private:
bool m_targetIsAligned;
bool m_overrideTargetAlignment;
}
Both threads have a pointer to TargetTracking object as data member.
Prior to the refactoring, the code was working correctly. However, the data member m_targetIsAligned was declared as a member of writingThread, and readingThread was calling and waiting for writingThread::waitForAlign() to return. The refactoring process included moving code form writingThread to TargetTracking class as shown above.
After some tests, I was only able to make the code work if the mutable keyword was added to the declaration of the two booleans
private:
mutable bool m_targetIsAligned;
mutable bool m_overrideTargetAlignment;
}
I have done some reading about the use of mutable; ie 'casting away const', however, I am lost as for why it is required in this case. Before using mutable, I tried wrapping the access to the booleans using mutex but that did not work either.
So my question is why the mutable attribute required here?
Thanks in advance
Daniel
I am doing some refactoring and I came across the following issue. I am running a multi-thread program, where a pair of threads (call the writingThread and readingThread) are accessing a shared object. One thread is processing an image and sets a data member in the shared object when the desired condition occurs while the second thread (readingThread) waits for the given condition (data member set to continue its process).
The shared object has the following declaration:
class TargetTracking : public QObject
{
...
public:
bool isTargetAligned() { return m_targetIsAligned; }
void overrideTargetAlignment( const bool action) { m_overrideTargetAlignment = action; }
void waitForAlign();
private:
bool m_targetIsAligned;
bool m_overrideTargetAlignment;
}
Both threads have a pointer to TargetTracking object as data member.
Prior to the refactoring, the code was working correctly. However, the data member m_targetIsAligned was declared as a member of writingThread, and readingThread was calling and waiting for writingThread::waitForAlign() to return. The refactoring process included moving code form writingThread to TargetTracking class as shown above.
After some tests, I was only able to make the code work if the mutable keyword was added to the declaration of the two booleans
private:
mutable bool m_targetIsAligned;
mutable bool m_overrideTargetAlignment;
}
I have done some reading about the use of mutable; ie 'casting away const', however, I am lost as for why it is required in this case. Before using mutable, I tried wrapping the access to the booleans using mutex but that did not work either.
So my question is why the mutable attribute required here?
Thanks in advance
Daniel