mardi 12 avril 2022

Detaching Threads in a loop - C++

I have a code where I call some GET and POST CURL functions in a loop. I want to call it and the result do not matter to me cause inside the function it's saves the result in a log file. The speed is crucial so I am detaching this functions like the code bellow:

for(;;){
    
  // other codes ///
  std::thread httpthread = std::thread([&]() {


    GETPOSTEvents GETEvent;
    string LogPath = logPathforerror;
                                                                    
    GETEvent.sendHttpEvent(quantity3_HTTPEvent, LogPath);

  });

  if (httpthread.joinable()) {
     httpthread.detach();
  }
    
  std::thread httpthread2 = std::thread([&]() {
        
    GETPOSTEvents GETEvent;
    string LogPath = logPathforerror;
                                                                           
    GETEvent.sendHttpEvent(quantity3_HTTPEvent2, LogPath);
        
  });
        
  if (httpthread2.joinable()) {
    httpthread2.detach();
  }
    
    
 // other codes ///

}

My question: is this safe? Is there any problem if the detached function do not ends before the loop?

Thanks!

Aucun commentaire:

Enregistrer un commentaire