Call appendPlainText without lock(example of advanced Qt programming)



  • 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



  • 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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.