5

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.

3
  • 2
    A 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 7, 2016 at 8:12
  • Consider the API differences; for example CreateThread in Windows vs pthread_create in *nix. Apr 7, 2016 at 8:45

2 Answers 2

14

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.

4
  • 1
    This is most likely the real answer.
    – 5gon12eder
    Apr 8, 2016 at 0:19
  • 1
    The C++ threading API is just a wrapper around implementation defined "native handles" and doesn't solve any platform dependencies.
    – Brendan
    Apr 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.
    – Jules
    Apr 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.
    – Clearer
    Apr 10, 2018 at 12:04
4

... 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.

1
  • - differences in synchronization (and caching). Apr 8, 2016 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.