QReadWriteLock locks for reading...but why?
Hi,
I have a big ressource and several Threads want to read the ressource simultanous. The ressource get exchanged every hour. While exchanging the read-access should be blocked. I solve this with QReadWriteLocker.
But why the Threads are locked for reading. If one thread reads a lot of data, all the other threads are locked, because of QReadLocker.
I just want to lock the threads, while exchanging...
Thanks, bye
Re: QReadWriteLock locks for reading...but why?
If you QReadWriteLock::lockForWrite() the file to write it blocks users in other threads (as documented) until you unlock().
What exactly does "exchanged" mean?
Re: QReadWriteLock locks for reading...but why?
Thanks for reply :)
Exchanged means renewed...
While renew() the read-access should be blocked....
Code:
void DataManager::renew(){
QList<Data*>* data = getnewdata();
//clear old data
delete this->data;
//assign new data
this->data = data;
}
Simultaneous read should be possible, but QReadLocker prevent this.
Re: QReadWriteLock locks for reading...but why?
Nobody can help?
I just want to know why several Threads aren't able to read the same container at the same time?
Re: QReadWriteLock locks for reading...but why?
Active QReadLockers don't block other read locks but an open write lock will.
Here's an example showing three concurrent read locks and no blocks:
Code:
#include <QtCore>
#include <QDebug>
Q_OBJECT
public:
: QObject(p
), m_num
(number
), m_lock
(lock
) {
}
public slots:
void process() {
qDebug
() <<
QString("Thread %1 started").
arg(m_num
);
for (int i = 0; i < 10; ++i) {
sleep(1);
qDebug
() <<
QString("Thread %1 reading").
arg(m_num
);
}
qDebug
() <<
QString("Thread %1 done").
arg(m_num
);
emit finished();
}
signals:
void finished();
private:
int m_num;
};
int main(int argc, char **argv) {
for (int i= 0; i < 3; ++i) {
SlowReader *worker = new SlowReader(i, &lock);
worker->moveToThread(thread);
QObject::connect(thread,
SIGNAL(started
()), worker,
SLOT(process
()));
QObject::connect(worker,
SIGNAL(finished
()), thread,
SLOT(quit
()));
QObject::connect(worker,
SIGNAL(finished
()), worker,
SLOT(deleteLater
()));
QObject::connect(thread,
SIGNAL(finished
()), thread,
SLOT(deleteLater
()));
thread->start();
}
return app.exec();
}
#include "main.moc"
Each thread holds a read lock for the entire 10 seconds it takes to do the 10 reads.
Re: QReadWriteLock locks for reading...but why?
Thank you ChrisW67 for the test app.
It convinced me. :)