From ede54f45af84438506564bf826e88d12c16639c4 Mon Sep 17 00:00:00 2001 From: Taegyun Kim Date: Wed, 4 Dec 2024 21:07:52 +0000 Subject: [PATCH] use pthread --- .../dd_wrapper/test/test_forking.cpp | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/ddtrace/internal/datadog/profiling/dd_wrapper/test/test_forking.cpp b/ddtrace/internal/datadog/profiling/dd_wrapper/test/test_forking.cpp index 717c3d2ab1c..73de71abbc2 100644 --- a/ddtrace/internal/datadog/profiling/dd_wrapper/test/test_forking.cpp +++ b/ddtrace/internal/datadog/profiling/dd_wrapper/test/test_forking.cpp @@ -3,14 +3,13 @@ #include #include +#include // Initiate an upload in a separate thread, otherwise we won't be mid-upload during fork -std::thread -upload_in_thread() -{ - auto t = std::thread([&]() { ddup_upload(); }); +void* upload_in_thread(void*) { + ddup_upload(); - return t; + return nullptr; } [[noreturn]] void @@ -55,7 +54,12 @@ sample_in_threads_and_fork(unsigned int num_threads, unsigned int sleep_time_ns) // Collect some profiling data for a few ms, then upload in a thread before forking std::this_thread::sleep_for(std::chrono::milliseconds(500)); join_samplers(threads, done); - auto upload_handle = upload_in_thread(); + + pthread_t thread; + if (pthread_create(&thread, nullptr, upload_in_thread, nullptr) != 0) { + std::cout << "failed to create thread" << std::endl; + std::exit(1); + } // Fork, wait in the parent for child to finish pid_t pid = fork(); @@ -64,7 +68,7 @@ sample_in_threads_and_fork(unsigned int num_threads, unsigned int sleep_time_ns) profile_in_child(num_threads, 500e3, done); // Child profiles for 500ms } - upload_handle.join(); + pthread_join(thread, nullptr); // Parent int status; @@ -93,7 +97,11 @@ fork_stress_test(unsigned int num_threads, unsigned int sleep_time_ns, unsigned launch_samplers(ids, sleep_time_ns, threads, done); std::vector children; - upload_in_thread(); + pthread_t thread; + if (pthread_create(&thread, nullptr, upload_in_thread, nullptr) != 0) { + std::cout << "failed to create thread" << std::endl; + std::exit(1); + } while (num_children > 0) { pid_t pid = fork(); if (pid == 0) { @@ -107,7 +115,7 @@ fork_stress_test(unsigned int num_threads, unsigned int sleep_time_ns, unsigned // Parent int status; done.store(true); - upload_in_thread(); + ddup_upload(); for (pid_t pid : children) { waitpid(pid, &status, 0); if (!is_exit_normal(status)) {