Take the 2-minute tour ×

From HERE I read that

A thread-safe function can be called simultaneously from multiple threads, even when the invocations use shared data, because all references to the shared data are serialized.

I know to make a segment of code thread safe we use locks so that only one thread at a time can access it. But here it says a thread safe function can be called simultaneously because all it's data are serialized. I don't understand how serializing the references can make a function thread safe? Thanks.

share|improve this question
"I know to make a segment of code thread safe we use locks so that only one thread at a time can access it." No, that's a serious misunderstanding. You make code thread safe by using locks to ensure that no thread can modify data while another thread is, or might be, accessing the same data. There is no problem with multiple threads accessing the same code if they operate on different data and you do have problems with threads, even if running completely different code, if they access the same data. –  David Schwartz Sep 10 '13 at 5:55
In this context, serialized is the opposite of concurrent. (And "references" has nothing to do with C++ "references", but has the common English meaning of referring to something.) –  molbdnilo Sep 10 '13 at 5:58
The correct term is 'sequentialised', not 'serialised', which has another meaning. –  EJP Sep 10 '13 at 10:15
the qt documentation in this instance was seriously negligent in assuming that ALL people would interpret the paragraph the same way the author meant it to be. It was unnecessarily ambiguous, which is a no-no when writing documentation! But combining the answer+comments above, you get what it really means, which is to access shared data, one at a time, like the drive through at McDonald’s, you cant just jump in front of each other after ordering because the orders would get mixed up. Sure you might get lucky a few times, since many order similar things, but a bad mix-up is bound to happen. –  osirisgothra Mar 3 at 19:26
add comment

3 Answers

up vote 2 down vote accepted


You have misunderstood slightly. Its not serializing the references but it is serializing the ACCESS to the references.

Now when you use a lock to protect a reference the access to the reference can only be used by single thread at a time. So it is serial and not parallel. And thus thread safe.

share|improve this answer
add comment

Serialization is sometimes synonym of locking. Read "Thread-Safety" part of this article, and you will see, that they use QMutexLocker to access global data. Why they call this serialization? Because all critical parts of the code are protected by mutex, and actually run serially, even when executed from different threads.

Sometimes Serialization in multi-threaded environment means redirecting concurrent requests to a single thread, which has incoming command queue, and handles these requests serially. The meaning is the same - concurrent requests are serialized, i.e. cannot be executed in parallel.

share|improve this answer
add comment

Well I'm guessing that by serializing you mean queueing, and the reason explanation you can find here:

Dead lock prevention

the general idea of thread safe is eliminating the resource sharing:

  1. by using queue to keep task in order, you can pop from queue each request, so handling of request is serialized and not parallel (that solve the question I hope).

  2. "hold and wait" - means that you take everything as a hole, and not request resource one by one

  3. "No preemption" - not always possible or effective, but it means that resource can be taken from a process that is already using it.

  4. "circular wait" - nice algorithm to number all resource, and always acquiring from low number to high number, causing "loop" request order, that there is always one that can complete the task and free the resource to the other.

share|improve this answer
add comment

Your Answer


