mercredi 26 avril 2017

How to create thread safe lib for C and C++ on Linux?

I want to write some library in C++11, which will be used in C and C++ programs (so all exported function will be marked as extern "C").

I want all exported functions to be thread-safe. So if I would write this library on windows, I can use Critical Section Objects, but what should I use in Linux?

Here's my options:

  1. POSIX mutexes
  2. C++11 std::mutex
  3. C++11 atomic variables
  4. Linux (POSIX) semaphores

Troubles: There is no guarantees of which thread - POSIX or C++11 - will be used in code which will use my library.

So 1) and 2) options cannot be used, because POSIX threads != C++11 threads.

Rest options look much slower than mutex-based solutions.

Does anybody know the correct way to create thread-safe library on Linux?

Aucun commentaire:

Enregistrer un commentaire