dimanche 8 octobre 2023

threads become immediately joinable upon fork [duplicate]

I would like to understand how join returns immediately for the child process below. I understand that fork() only results in cloning the thread that executed it but what happens to the other threads. Specifically the platform I'm interested in is Linux 3.x and later. I'd like to understand how the threading library and the kernel interface to mark the remaining threads as being immediately joinable.

The code was built using g++ version 11.3 on linux 5.17. g++ -pthread threads_test.cc

#include <thread>
#include <unistd.h>
#include <chrono>
#include <iostream>

using namespace std;

int main() { 
   std::thread t1([]() { 
      std::this_thread::sleep_for(5s);
      std::cout << std::this_thread::get_id() << " in thread..\n";
   });

   fork();

   t1.join();
   std::cout << "Joined\n";

}
Joined
139782025066048 in thread..
Joined

Aucun commentaire:

Enregistrer un commentaire