google / google/googletest

Preprocessor macros GTEST_IS_THREADSAFE and GTEST_HAS_PTHREAD do not work as expected

Open
#3,586 9 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
39.5k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

**Describe the bug**

At `./googletest/README.md` (not at `./README.md`) you find:

>### Multi-threaded Tests
>
>GoogleTest is thread-safe where the pthread library is available. After `#include "gtest/gtest.h"`, you can check the`GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is `#defined` to 1, no if it's undefined.).
>
>If GoogleTest doesn't correctly detect whether pthread is available in your environment, you can force it with
>
> -DGTEST_HAS_PTHREAD=1
>
>or
>
> -DGTEST_HAS_PTHREAD=0

GTEST_IS_THREADSAFE always compiles to "yes" (macro is defined) no matter if PThreads are available or not.

cmake option -DGTEST_HAS_PTHREAD has no effect no matter if set to 0 or 1.

I expect that both macros work as documented.

**Steps to reproduce the bug**

Check GTEST_IS_THREADSAFE on every compiler that supports preprocessor macros, for example with GCC:

#ifdef GTEST_IS_THREADSAFE
#warning "pthread is available"
#else
#warning "pthread is NOT available"
#endif

always compiles to

/home/ingo/devel/googletest/test_simple.cpp:18:2: warning: #warning "pthread is available" [-Wcpp]
18 | #warning "pthread is available"
| ^~~~~~~

With GTEST_HAS_PTHREAD try to configure:

~$ cmake -S . -B build -DGTEST_HAS_PTHREAD=0|1

Alternative you may compare configuring with `-D gtest_disable_pthreads=ON|OFF`

**Does the bug persist in the most recent commit?**

Yes.

**What operating system and version are you using?**

Any operating system that supports a compiler that can expand preprocessor macros.

**What compiler and version are you using?**

~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20210110 (Debian 10.2.1-6)

**What build system are you using?**

~$ cmake --version
cmake version 3.18.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Contributor guide

Open the contributing guide

Research direction

Start with the macro behavior described in googletest/README.md and reproduce it using the provided preprocessor check. Trace how GTEST_IS_THREADSAFE and GTEST_HAS_PTHREAD are configured through the CMake options gtest_disable_pthreads and -DGTEST_HAS_PTHREAD. Done means detection and both forced settings produce the documented macro results, verified on supported builds.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.