Skip to content

Commit

Permalink
use pthread
Browse files Browse the repository at this point in the history
  • Loading branch information
taegyunkim committed Dec 4, 2024
1 parent 9a41c0a commit ede54f4
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
#include <gtest/gtest.h>

#include <chrono>
#include <pthread.h>

// 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
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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<pid_t> 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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit ede54f4

Please sign in to comment.