third-party/rrdtool-1.4.8/doc/rrdthreads.html
rrdthreads - Provisions for linking the RRD library to use in multi-threaded programs
Using librrd in multi-threaded programs requires some extra precautions, as the RRD library in its original form was not thread-safe at all. This document describes requirements and pitfalls on the way to use the multi-threaded version of librrd in your own programs. It also gives hints for future RRD development to keep the library thread-safe.
Currently only some RRD operations are implemented in a thread-safe way. They all end in the usual "_r" suffix.
In order to use librrd in multi-threaded programs you must:
Link with librrd_th instead of librrd (use -lrrd_th when linking)
Use the "_r" functions instead of the normal API-functions
Do not use any at-style time specifications. Parsing of such time specifications is terribly non-thread-safe.
Never use non *_r functions unless it is explicitly documented that the function is tread-safe.
Every thread SHOULD call rrd_get_context() before its first call to any librrd_th function in order to set up thread specific data. This is not strictly required, but it is the only way to test if memory allocation can be done by this function. Otherwise the program may die with a SIGSEGV in a low-memory situation.
Always call rrd_error_clear() before any call to the library. Otherwise the call might fail due to some earlier error.
Some precautions must be followed when developing RRD from now on:
Only use thread-safe functions in library code. Many often used libc functions aren't thread-safe. Take care in the following situations or when using the following library functions:
A header file named rrd_is_thread_safe.h is provided that works with the GNU C-preprocessor to "poison" some of the most common non-thread-safe functions using the #pragma GCC poison directive. Just include this header in source files you want to keep thread-safe.
Do not introduce global variables!
Do not use getopt or getopt_long in *_r (neither directly nor indirectly).
Do not use the rrd_parsetime function!
Currently there exist thread-safe variants of rrd_update, rrd_create, rrd_dump, rrd_info, rrd_last, and rrd_fetch.
Peter Stamfest <[email protected]>