Is it safe for QtConcurrent::run() to call other functions
-
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.
-
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.
-
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?
-
"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.