Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Call appendPlainText without lock(example of advanced Qt programming)
QtWS25 Last Chance

Call appendPlainText without lock(example of advanced Qt programming)

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved