Command-line options to force googletest to run in single thread

There's no commandline switch for single/multi-threading. libgtest is built either single-threading or multi-threading.

To make it single-threading, build gtest with ./configure --with-pthreads=no, then link your unit test app without -pthread

If you only want single threading sometimes, then make a no-pthreads build of libgtest, call it something else, and link it when you want.


If you are building googletest with cmake, you can use the cmake option gtest_disable_pthreads to control support for threading. For example:

$ mkdir build && cd build
$ cmake .. -Dgtest_disable_pthreads=ON

The resulting output should not show:

-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  

Then you can run make as usual.


Another option short of rebuilding with multi-threading disabled is to simply create a test fixture for tests that cannot run concurrently. Then in the SetUp() and TearDown() methods, lock and unlock a mutex respectivey.

Be sure to use a mutex that exists outside of the test fixture, as the fixture is created and torn down for every test.

std::mutex g_singleThread;

class SingleThreadedTests
{
protected:
   virtual void SetUp() override
   {
      g_singleThread.lock();
   }

   virtual void TearDown() override
   {
      g_singleThread.unlock();
   }
};

TEST_F(SingleThreadedTests, MyTest)
{
   // ...
}