Is there a way to check if thread-safe functions are available in the C standard library?

As far as thread-safe functions in newer versions of the C standard library are concerned , is there a cross-platform way to find out if these are available through a pre-defined processor? I mean features like localtime_r()

.

If there is no standard way, what's the reliable way in GCC? [EDIT] Or posix systems with unistd.h?

+3


source to share


1 answer


There is no standard way to test this, which means there is no way to test it on all platforms. Tools like autoconf

will create a tiny C program that calls this function and then try to compile and link it. This works, it looks like the function exists, if not, then it may not exist (or the compiler options are incorrect and the appropriate must be set CFLAGS

).

So you basically have 6 options:



  • Demand their existence. Your code can only work on platforms where they exist; period. If they do not exist, compilation will fail, but this is not your problem as the platform violates your minimum requirements.

  • Don't use them. If you are using non-thread safe, it may be protected by a global lock (like a mutex), it doesn't matter if they exist or not. Of course, your code will only work on platforms with POSIX mutexes, however, if there are no POSIX mutexes on the platform, it also won't have POSIX threads, and if it doesn't have POSIX threads (and I think you probably use POSIX threads w / o support for any alternative) why do you need to worry about thread safety in the first place?

  • Decide at runtime. Depending on the platform, either do "loose coupling" so you can test it at runtime if the function was found or not (the function pointer will point to NULL

    if it is not) or alternatively resolve the symbol dynamically using something like dlsym()

    (which is also not very portable, but widely supported in the Linux / UNIX world). However, in this case you will need a fallback if the function is not found at runtime.

  • Use a tool like autoconf

    another tool with similar functionality, or your own script configuration to determine this before compiling (and possibly setting preprocessor macros depending on the result). In this case, you will also need a fallback solution.

  • Limit the use of known platforms. It is known if this feature is available on a specific platform (and once it is available it will not disappear in the future). Most platforms expose preprocessor macros to check which platform, and sometimes which version. For example. if you know GNU / Linux, Android, Free / Open / NetBSD, Solaris, iOS, and MacOS X offer this feature, test if you compile one of these platforms, and if so, use it. If the code is compiled for a different platform (or if you cannot determine which platform), it may or may not offer this feature, but since you cannot say for sure, it is better to be safe and use a backup.

    / li>
  • Let the user decide. Either always use a fallback unless the user signals support or does it the other way around (which probably makes more sense), always assume it is there, and in the event of a compilation failure, suggest a way the user can force your code to "match "mode" to indicate that thread-safe functions are not available (for example, by setting an environment variable or using another target.) Of course, this is the least convenient method for the (bad) user.

+3


source







All Articles