@alexisdm thanks for the idea. What about this solution:
class MyQThread:
public: QThread
{
...
QList<QString> list;
QMutex mutex;
QWaitCondition listNotEmpty;
}
void MyQThread::write(QString text)
{
QMutexLocker locker(&mutex);
list.append(text);
listNotEmpty.wakeAll();
}
void MyQThread::run()
{
forever
{
mutex.lock();
if(list.isEmpty())
listNotEmpty.wait(&mutex);
if(list.isEmpty())
{
mutex.unlock();
continue;
}
QString text = list.takeFirst();
mutex.unlock();
...//do something
}
}
And what about the second parameter of wait()
- unsigned long time = ULONG_MAX
.
It seems that in my case it will not be error, when write()
method is not called for a long time and wait()
will return false
in run()
after ULONG_MAX
. And in this case I need just to wait again...
And in documentation is written:
The mutex will be returned to the same locked state.
Does it mean, that mutex will be always locked after wait()
even if mutex was not locked before wait()
or wait()
returns false?