4 ms·
The inability to detect musl is indeed frustrating. One example of this problem is in detecting C11 thread support. This is a feature of the C library, not the
by ludocode 7y ago
The inability to detect musl is indeed frustrating.
One example of this problem is in detecting C11 thread support. This is a feature of the C library, not the compiler, but it's the compiler that sets C11 feature flags. For threads, you can only detect that they aren't supported by __STDC_NO_THREADS__. But of course since this can't be defined retroactively in old versions of musl, you can't use it to detect C11 thread support in musl. And since you can't detect musl at all, you can't even just assume the version of musl is recent enough and turn it on unconditionally.
The musl team seems to advocate guessing at supported features by detecting with test compilation (a.k.a. configure scripts.) They don't believe in depending on specific versions of musl because features can be backported or disabled. Okay fine, but then why not give us explicit means to detect it? Why not just define MUSL_C11_THREADS or something so we can detect it at compile-time instead of during a slow, fuzzy, non-portable configure step?
Does anyone know of a workaround for this? Does musl define any kind of feature macro that can be used to detect C11 thread support?
- dooglius 7y agoFunny, GCC/glibc had this same problem (except you could work around it by detecting the version).
- api 7y agoI've never had a problem with C++ on musl/Alpine. ZeroTier ports fine to that target. In fact we use Alpine/musl images to build static binaries for ancient Linux distributions that lack new enough distro-native compilers.
- tlb 7y agoDo you have a real system that uses threads if available, but can also work without them? What does the architecture look like?
- Hello71 7y agothere are other threading options available than C11.
- toast0 7y agoBEAM, the Erlang virtual machine, could be compiled with or without threads until release 21 (from 2018).
- com2kid 7y agoPlenty of embedded libraries of optional thread support, though from what I've seen this option is explicitly enabled by setting a define specific to that library, and also passing in other needed info/function pointers since threading can vary so much on embedded systems.
- Arelius 7y agoThere are many systems that I've worked on that do this in practice. So firstly, a common use-case of threads is for improving performance. Using a job system is a good mechanism for this that doesn't strongly couple the architecture to the details of number of threads, or etc. Often this can be done with a fork-join model, where when you have a big chunk of work to do, you can create a bunch of jobs to do some work, then either wait on them immediately, or go off and do some other work while you wait, either approach works since it's possible to have a job system that will do work on the waiting thread. At this point it's then pretty simple to either build in a fallback either directly into the job system, where you either do the work immediately upon launching it, or upon waiting on it. or it's trivial to detect presence of threads in the work code, and replace the job code with a simple for loop or something similar. This is far from the only style of system I've seen that can work optionally with threads, but it's one I've seen working to good effect.
- zamadatix 7y agoDepending on your compiler __has_include("threads.h") combined with a check for thrd_t or similar after should cover everything except the craziest cases.
- ludocode 7y agoThanks, this looks like it will work.
- nialv7 7y agoThis is just C being generally bad at any feature detection. This is why the "./configure" exists, and you check for the existence of headers and functions there.