lundi 21 juin 2021

Why would C++'s thread constructor call the thread destructor? [closed]

I have a multi-threaded, C++, unit-test program that is executed multiple times in a bash(1) while loop. It crashes sometime after 6,000 executions because a call to std::thread::thread() calls std::thread::~thread(). I would like to understand why.

Here's the stack trace of the thread that causes the crash (the names have been changed to protect the guilty). Note that the thread constructor in frame 6 calls the thread destructor in frame 5, which causes the process to crash.

(gdb) where
#0  0x00007fae5f5923d7 in raise () from /lib64/libc.so.6
#1  0x00007fae5f593ac8 in abort () from /lib64/libc.so.6
#2  0x0000000000408984 in myTerminate () at .../Foo_test.cpp:282
#3  0x00007fae5fea0746 in ?? () from /lib64/libstdc++.so.6
#4  0x00007fae5fea0773 in std::terminate() () from /lib64/libstdc++.so.6
#5  0x000000000040b735 in std::thread::~thread (this=0x7ffd283f6bf0, __in_chrg=<optimized out>) at /usr/include/c++/4.8.2/thread:143
#6  0x000000000040889a in (anonymous namespace)::FooTest_Bar_Test::TestBody (this=0x1571d40) at .../Foo_test.cpp:237
#7  0x00007fae6061e7a3 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) () from /lib64/libgtest.so.0
#8  0x00007fae60614b27 in testing::Test::Run() () from /lib64/libgtest.so.0
#9  0x00007fae60614bce in testing::TestInfo::Run() () from /lib64/libgtest.so.0
#10 0x00007fae60614cd5 in testing::TestCase::Run() () from /lib64/libgtest.so.0
#11 0x00007fae60618018 in testing::internal::UnitTestImpl::RunAllTests() () from /lib64/libgtest.so.0
#12 0x00007fae606182a7 in testing::UnitTest::Run() () from /lib64/libgtest.so.0
#13 0x0000000000408ae9 in main (argc=1, argv=0x7ffd283f6fa8) at .../Foo_test.cpp:292

Here's the frame 6 code. Line 237 is the relevant one.

(gdb) list
232 // Tests bar
233 TEST_F(Foo, Bar)
234 {
235     // Do something on separate thread
236     Foo foo{};
237     std::thread fooThread(&FooTest::startFoo, this, std::ref(foo));
238
239     ...
240 
241     {

The myTerminate() function I created isn't much help:

... FATAL Foo_test.cpp:268:myTerminate    terminate() called without an active exception

Because the unit-test program executes successfully thousands of times before crashing in this unusual way, I can't help but wonder if the operating system is the cause (e.g., due to some sort of resource exhaustion).

My system is a CentOS 7 VM. The compiler is g++ 4.8.5.

Have you ever seen anything like this? Do you have any idea what could be the problem?

Aucun commentaire:

Enregistrer un commentaire