Jazzwave06
Jazzwave06

Reputation: 1851

Is it possible to turn off thread safety in C++?

I'm building an IO intensive distributed system, and I plan to make the process stateless in order to provide a single threaded, but scalable, runtime. I started the project in C, with libuv, and it worked great, with awesome performances. However, the development is taking much time, as C requires a lot of boilerplate code.

Therefore, I'm evaluating C++ as an alternative, however, I haven't found any way to opt out of thread safe structure such as std::shared_ptr. Is there any way, in clang or gcc, to disable atomic access to the standard library structure, as to have a single threaded process without any mutex/atomic overhead?

Upvotes: 3

Views: 1836

Answers (3)

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

Standard library thread-safety: Libstdc++

As Florian said that the thread-safety of libstdc++'s reference counting depends on whether the program is linked to libpthread, so that single-threaded programs (or more specifically, ones that aren't linked to libpthread) automatically disable thread-safety. Libstdc++ uses reference counting in shared_ptr, the old Copy-on-Write std::string, and in std::locale initialization. There are a few ways to force it to not use non-thread-safe reference counting:

  • Avoid any libpthread dependency, so that libstdc++ code will use the single-threaded mode automatically.
  • Rebuild GCC with --disable-thread=single so that the rebuilt libstdc++ uses single-threaded mode by default, even when the program is linked to libpthread.
  • Use a non-standard property of libstdc++'s shared_ptr implementation to force non-atomic reference counting even when GCC is not built in single-threaded mode and the program is linked to libpthread, see https://stackoverflow.com/a/15141844/981959 for how to do that. This doesn't affect other uses of reference counting in locales and COW strings.

Standard library thread-safety: Libc++

For libc++ I think you need to rebuild the library, passing the -DLIBCXX_ENABLE_THREADS=OFF option to CMake. That will globally disable all thread-safety. You can't change that property when using the library, it's fixed when libc++ is built.

Core language thread-safety: GCC and Clang

You can use the -fno-threadsafe-statics option to disable the thread-safe initialization of local static variables. The code to make initialization of local statics thread-safe is added automatically unless you use that option, and is independent of whether the program is linked to libpthread or not.

Upvotes: 3

Gem Taylor
Gem Taylor

Reputation: 5613

One way might be to go for a pre c++11 standard, where the stl and language as specified was explicitly not threadsafe in a number of ways, such as singleton construction. Of course the local platform libraries provide ways to launch threads, and provide thread-safe ways to perform memory allocation, but discussion of that is outside the scope of the question.

Alternatively, just don't use the supplied shared_ptr if it offends you that much. Most of the rest of the "old" stl does not depend on it and does not provide any thread safety out the box. You clearly don't want to look at std::async and friends.

About the only other place you get under the hood threaded behaviour is static function variables, i.e. singletons, and the memory manager (malloc), though that will be hard to avoid.

Upvotes: -1

Florian Weimer
Florian Weimer

Reputation: 33694

The libstdc++ implementation of std::shared_ptr automatically disables atomic instructions if the process is not linked against libpthread. You can check with ldd whether this is the case.

Typical builds of libuv are linked against libpthread, so you will need a build of the library which does not do that (if that it is even possible).

Upvotes: 2

Related Questions