Call appendPlainText without lock(example of advanced Qt programming)
-
wrote on 2 Aug 2012, 16:12 last edited by
On pg256 and pg260 of the book "advanced Qt programming"
The author do not lock the function "appendPlainText" and the increment
of integer(++done).The document do not mention that the ""appendPlainText"
function of the "QPlainText" is thread safe, and C++98 do not guarantee
atomic, so why do we don't need to lock them?Thanks -
wrote on 3 Aug 2012, 01:34 last edited by
Hi,
All the codes are called in the main thread, so no lock is needed.
Event System of Qt is thread safe which is used by the code you mentioned.
@
ConvertImageTask::run()
{
...
QMetaObject::invokeMethod(...
}
@@
void convertImages(...)
{
...
QApplicaton::postEvent(...
@
BTW, members of QWidget normally doesn't thread safe, so QPlainTextEdit::appendPlainText() can only be called in main thread.
-
wrote on 3 Aug 2012, 03:34 last edited by
Do you means
[code]
QMetaObject::invokeMethod(...
[/code]and
[code]
QApplicaton::postEvent(...
[/code]are thread safe if I invoke the function on main thread?
Thank you very much
2/3