Next: , Previous: Initializing the library, Up: Preparation


2.5 Multi-Threading

As mentioned earlier, the Libgcrypt library is thread-safe if you adhere to the following requirements:

Libgcrypt contains convenient macros, which define the necessary thread callbacks for PThread and for GNU Pth:

GCRY_THREAD_OPTION_PTH_IMPL
This macro defines the following (static) symbols: gcry_pth_init, gcry_pth_mutex_init, gcry_pth_mutex_destroy, gcry_pth_mutex_lock, gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, gcry_pth_select, gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect, gcry_threads_pth.

After including this macro, gcry_control() shall be used with a command of GCRYCTL_SET_THREAD_CBS in order to register the thread callback structure named “gcry_threads_pth”. Example:

            ret = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);

GCRY_THREAD_OPTION_PTHREAD_IMPL
This macro defines the following (static) symbols: gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock, gcry_threads_pthread.

After including this macro, gcry_control() shall be used with a command of GCRYCTL_SET_THREAD_CBS in order to register the thread callback structure named “gcry_threads_pthread”. Example:

            ret = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);

Note that these macros need to be terminated with a semicolon. Keep in mind that these are convenient macros for C programmers; C++ programmers might have to wrap these macros in an “extern C” body.


Footnotes

[1] At least this is true for POSIX threads, as pthread_create is a function that synchronizes memory with respects to other threads. There are many functions which have this property, a complete list can be found in POSIX, IEEE Std 1003.1-2003, Base Definitions, Issue 6, in the definition of the term “Memory Synchronization”. For other thread packages, more relaxed or more strict rules may apply.