-2

Is library libgcrypt thread-safe one? In case that I don't divide resources of this library between threads, but create different context in every thread. C++ programming language.

user3107137
  • 15
  • 1
  • 3

1 Answers1

0

Documentation says, libgcrypt is thread-safe with conditions:

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

  • If you use pthread and your applications forks and does not directly call exec (even calling stdio functions), all kind of problems may occur. Future versions of Libgcrypt will try to cleanup using pthread_atfork but even that may lead to problems. This is a common problem with almost all applications using pthread and fork.
  • The function gcry_check_version must be called before any other function in the library. To achieve this in multi-threaded programs, you must synchronize the memory with respect to other threads that also want to use Libgcrypt. For this, it is sufficient to call gcry_check_version before creating the other threads using Libgcrypt1.
  • Just like the function gpg_strerror, the function gcry_strerror is not thread safe. You have to use gpg_strerror_r instead.

With an additional caveat:

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.

P.P
  • 117,907
  • 20
  • 175
  • 238