I am reading book "Foundations of Qt Development", Chapter 12, and I read that threading is platform-dependent, can someone explain, why? I know how to use threads but that what they said is not so clear to me.
-
related (possibly a duplicate): [are multithreading, multiprocessing, multitasking implemented at instruction set or OS?](http://programmers.stackexchange.com/questions/271346/are-multithreading-multiprocessing-multitasking-implemented-at-instruction-set) – gnat Apr 07 '16 at 08:08
-
2A better way to say that is "there are platform-specific differences". It is possible to wrap the basic threading facilities in a platform-agnostic ways, as multiple languages are. Also, inter-thread communication (in a thread-safe way) are also platform-specific, for which "wrapping" is more tricky, and requires its users to know about those trickiness. – rwong Apr 07 '16 at 08:12
-
Consider the API differences; for example [`CreateThread`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx) in Windows vs [`pthread_create`](http://linux.die.net/man/3/pthread_create) in *nix. – Ben Cottrell Apr 07 '16 at 08:45
2 Answers
The book you are reading was published in 2007. The C++ API for managing threads wasn't standardised until 2011. At the time, on different systems you had to use entirely different platform-specific libraries (pthreads, win32 threads, etc). Now, this is no longer true. Your book is out of date.

- 17,614
- 2
- 33
- 63
-
1
-
1The C++ threading API is just a wrapper around implementation defined "native handles" and doesn't solve any platform dependencies. – Brendan Apr 08 '16 at 12:50
-
4@Brendan The same is true, for example, of IO streams, as well. Having a standardised API "solves platform dependencies" in the sense that it means you can write code that will compile and run on different systems. It doesn't mean that what runs is *identical*, but in most circumstances, that isn't a requirement. – Jules Apr 08 '16 at 13:53
-
@Brendan By that logic, nothing we do is platform independent. Your compiler writes binaries that depend on your cpu after all. – Clearer Apr 10 '18 at 12:04
... threading is platform-dependent, can someone explain, why?
The platform dependencies are typically differences in the way that the thread scheduler works. The fact is that thread schedulers do behave differently on different platforms due to:
- differences in the thread scheduler algorithms across different operating systems and versions.
- differences in scheduler tuning parameters; e.g. the duration of a "timeslice", whether / how the scheduler considers past thread behavior when scheduling, etc
- differences in hardware platforms; e.g. numbers of cores, etc
- differences in compilers, etc leading to differences in behaviour when threads share data structures (especially if you don't follow the rules for proper synchronization)
- differences in workload, including what else is running on the system apart from (say) the Qt application.
Why are there differences? Well, it is hard to see how one could avoid differences! It is impractical / impossible to standardize platforms to the degree that thread schedulers will behave the same.
The other aspect to platform dependency is that historically there have been a number of different C / C++ APIs for threading. (To get a feeling for the scale of this, see https://en.wikipedia.org/wiki/List_of_C%2B%2B_multi-threading_libraries.)
This is less of a problem now that there is an official C++ threading library, unless you are working on an old code-base and / or using an older C++ compiler / libraries.

- 25,180
- 6
- 64
- 87