2
I’m trying to close a thread, but it appears the message "Process terminating with default action of Signal 6 (SIGABRT)", with memory error including Leak. How hard I try to execute:
valgrind --leak-check=full --show-leak-kinds=all --tool=memcheck --leak-check=yes --show-reachable=yes ./thread-test
The code below is what I’m trying to execute.
#include <CUnit/Basic.h>
#include <iostream>
#include <thread>
#include <future>
#include <chrono>
void console(const char *message) {
if (message == nullptr) return;
fprintf(stdout, "%s\r\n", message);
fflush(stdout);
}
void runNestedThread() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
console("Thread [B] running");
}
}
void nestedThread() {
std::thread threadB(runNestedThread);
threadB.join();
}
void runThread() {
nestedThread();
}
void testThread() {
std::thread threadA(runThread);
auto r = threadA.native_handle();
std::atomic<bool> done(false);
std::thread t([&done] {
done = true;
});
threadA.detach();
console("Starting Thread [A]");
int s = 0;
while (s++ < 5) std::this_thread::sleep_for(std::chrono::seconds(1));
pthread_cancel(r);
while (!done) std::this_thread::sleep_for(std::chrono::seconds(1));
if (done) console("Thread [A] finished");
}
int main(int argc, char **argv) {
CU_pSuite pSuite = nullptr;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("Utils", nullptr, nullptr);
if (nullptr == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if (nullptr == CU_add_test(pSuite, "Test Thread", testThread)) {
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}
two possibilities: a variable to control the loop of the thread, or abort it, the first seems to be correct, and faster, for sure, the thread will be finished immediately upon completion of the function.
– anon