
Originally Posted by
Sheng
Thanks, you are right. But I think it is dangerous to do that, even when A is singleton class, it might cause infinite recursive call in some case.
Let's see.
I have class A. It implement some calculate process:
class A {
public:
A();
void setData(const sometype __d);
void calc();
private:
sometype data;
};
void A::setData(const sometype __d) {
if(data_mutex.tryLock()) // error to user;
data=__d;
data_mutex.unlock();
}
void A::calc() {
if(!data_mutex.tryLock()) {
QMessageBox::warning(NULL,
"Lock",
"Data is still locked!");
return;
}
while(1) {
///calculate
}
data_mutex.unlock();
}
class A {
public:
A();
void setData(const sometype __d);
void calc();
private:
sometype data;
QMutex data_mutex;
};
void A::setData(const sometype __d) {
if(data_mutex.tryLock()) // error to user;
data=__d;
data_mutex.unlock();
}
void A::calc() {
if(!data_mutex.tryLock()) {
QMessageBox::warning(NULL,"Lock","Data is still locked!");
return;
}
while(1) {
///calculate
}
data_mutex.unlock();
}
To copy to clipboard, switch view to plain text mode
Can show to me on an example of this class, what sort of a problem can arise? Also what you understand as infinite recursive calls? You can loss data? Two (or more) threads can go into conditional race?
Please, specify peace of code where you plan to use such classes.
Bookmarks