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. Is it safe for QtConcurrent::run() to call other functions
Forum Updated to NodeBB v4.3 + New Features

Is it safe for QtConcurrent::run() to call other functions

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    qttester5
    wrote on last edited by
    #1

    I'm still trying to understand how threading works; if I use run() to call a function in another thread, is it safe for that function to also call other functions? I am not mutating anything, but I want to make sure I understand the flow when you put an operation on another thread. I assume it is fine to call other functions, and that those functions will run on the same thread as you called them from, is that correct?

    The functions I'd be calling would be either non-member functions or static member functions. But the function I'd be calling directly from run() would be a non-member function that would then call these other functions.

    1 Reply Last reply
    0
    • JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      Start off by reading on "Reentrancy and Thread-Safety":http://qt-project.org/doc/qt-5/threads-reentrancy.html.

      A function's member-ness and static-ness is not important. What's important is a its thread-safety.

      • It is safe for QtConcurrent::run() to call other thread-safe functions all the time.
      • It is safe for QtConcurrent::run() to call other reentrant functions... if you can guarantee that no other threads will call the same function or mutate the object that the function is a member of.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qttester5
        wrote on last edited by
        #3

        Thanks, I just read that guide. I'm wondering if in general, when they refer to "access" to data from different threads, if they are specifically referring to "writing" or mutating that data? In other words, if I pass a pointer to a single object around between threads, I would think this is thread safe even if they all "access" it at the same time provided that they are not actually changing that data, but merely reading it. Is this correct?

        Similarly, if I am calling other functions from within a thread, I would think it is safe for those other functions to get accessed simultaneously across many threads provided that those functions are not altering data, just reading data and returning new data. Is this a fair understanding of thread safety?

        1 Reply Last reply
        0
        • JKSHJ Online
          JKSHJ Online
          JKSH
          Moderators
          wrote on last edited by
          #4

          "Access" refers to both reading and writing.

          See "Synchronizing Threads":http://qt-project.org/doc/qt-5/threads-synchronizing.html for further discussion, and for details on how to protect shared data and make functions thread-safe. Pay particular attention to the paragraph about QReadWriteLock -- that should clarify things for you.

          [quote] if I pass a pointer to a single object around between threads, I would think this is thread safe even if they all “access” it at the same time provided that they are not actually changing that data, but merely reading it. Is this correct?

          Similarly, if I am calling other functions from within a thread, I would think it is safe for those other functions to get accessed simultaneously across many threads provided that those functions are not altering data, just reading data and returning new data. Is this a fair understanding of thread safety?[/quote]Yes, that's a fair understanding of the topic.

          There's one more little (but important) detail to add: It's not enough to say "those functions only read, but they don't alter any data". You must also make sure that the data that those functions read must not be altered by other functions during the reads.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          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