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.
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.
-
1
-
1The C++ threading API is just a wrapper around implementation defined "native handles" and doesn't solve any platform dependencies.– BrendanApr 8, 2016 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.– JulesApr 8, 2016 at 13:53
-
@Brendan By that logic, nothing we do is platform independent. Your compiler writes binaries that depend on your cpu after all.– ClearerApr 10, 2018 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.
CreateThread
in Windows vspthread_create
in *nix.