nemequ / hedley

A C/C++ header to help move #ifdefs out of your code
https://nemequ.github.io/hedley/
Creative Commons Zero v1.0 Universal
774 stars 51 forks source link

Adding _Thread_local #19

Closed rockdaboot closed 5 years ago

rockdaboot commented 5 years ago

Something like that would be handy: https://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably

nemequ commented 5 years ago

You're right, that would be very handy. Unfortunately it doesn't quite fit in Hedley right now; TLS is actually the example I used in the "Can you add support for «feature»?" FAQ:

Maybe. The first rule is that if a feature isn't known to be supported it must be safe to fall back on pure C89. For example, it's not a big deal if the compiler doesn't have an implementation for HEDLEY_INLINE; the macro is compiled down to nothing and the function may not be inlined, but that will not cause the code to stop functioning.

On the other hand, we can't add support for thread-local storage because if the compiler doesn't support it and we simply don't emit a TLS modifier (__thread, _Thread_local, __declspec(thread), etc.) the behavior is completely different and the resulting program will almost certainly be broken.

If a feature isn't a good fit for Hedley it very well may be a good fit for Portable Snippets.

For TLS, while I'd be willing to add something to portable-snippets, I think a better place to look would be TinyCThread (which I maintain) or, if you're using C++, TinyThread++.

Basically, the idea is that if something is in Hedley, you can use it without having to think about it or add a bunch of ifdefs (I want the preprocessor logic to be in Hedley, not code using Hedley). That said, I admit that it's a pretty onerous limitation, and it's something I may drop at some point in the future in favor of just only defining, for example, HEDLEY_THREAD_LOCAL if a TLS modifier is supported by the compiler.

I'm already thinking about stretching it to allow for things like HEDLEY_FEATURE_STATEMENT_EXPRESSIONS, which would be defined if statement expressions are supported and undefined otherwise. From that point it's an even smaller step to something like HEDLEY_THREAD_LOCAL.

Another possibility would be a second header, or even a second library, for optional stuff like this.

I'd be very interested to hear what people would like me to do here, so maybe we could use this issue to vote… if you want to keep the restriction in place, use the thumbs up. If you want to drop it and allow things like HEDLEY_THREAD_LOCAL even if it won't work everywhere, thumbs down. If you would rather have stuff like HEDLEY_THREAD_LOCAL in a separate header, let's go with the rocket icon.

Meanwhile, I'm going to close this issue. If we do end up allowing it, TLS will probably be the first thing I add (there is a reason I used it as the example in the FAQ); you don't need to worry about me forgetting about it.

rockdaboot commented 5 years ago

Thanks for your detailed answer !