Next: Client examples, Previous: Preparation, Up: How to use GnuTLS in applications
Although the GnuTLS library is thread safe by design, some parts of the crypto backend, such as the random generator, are not. Since libgcrypt 1.1.92 there was an automatic detection of the thread library used by the application, so most applications wouldn't need to do any changes to ensure thread-safety. Due to the unportability of the automatic thread detection, this was removed from later releases of libgcrypt, so applications have now to register callback functions to ensure proper locking in sensitive parts of libgcrypt.
There are helper macros to help you properly initialize the libraries. Examples are shown below.
#include <gnutls.h> #include <gcrypt.h> #include <errno.h> #include <pthread.h> GCRY_THREAD_OPTION_PTHREAD_IMPL; int main() { /* The order matters. */ gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); gnutls_global_init(); }
#include <gnutls.h> #include <gcrypt.h> #include <errno.h> #include <pth.h> GCRY_THREAD_OPTION_PTH_IMPL; int main() { gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth); gnutls_global_init(); }
/* The gcry_thread_cbs structure must have been * initialized. */ static struct gcry_thread_cbs gcry_threads_other = { ... }; int main() { gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_other); }