How to save the process?

I have a process that starts several threads that do some things, listen on some ports, etc.

After starting all threads, the main thread currently goes into an endless loop:

This is something like:

int main()
{
   //start threads
   while (true)
   {
      sleep(1000);
   }
}

Optional sleepensures that the main thread is not powered by the processor.

Is this approach appropriate? Is there an industry standard for how a process is supported by alivet? Thank.

EDIT: some explanation:

  • threads are listeners, therefore joinor WaitForSingleObjectnot an option. Normally I could use the connection here, but the threads are started by the third client library, and I do not control them.
  • Performing some processing in the main thread does not make sense from a design point of view.
+3
3

. Linux Daemon Writing HOWTO, , - :

int main() {
    pid_t pid;

    /* Fork off the parent process */       
    pid = fork();
    if (pid < 0) {
            exit(EXIT_FAILURE);
    }
    /* If we got a good PID, then
       we can exit the parent process. */
    if (pid > 0) {
            exit(EXIT_SUCCESS);
    }
    // now start threads & do the work

    for( thread *t : threads ) {
            join( t );
    }

    return 0;
}

, , , . , .

+3

, :

int main( ) {
  // start threads
  for( thread *t : threads ) {
    join( t );
  }
  // finalize everything or restart the thread
  return 0;
}

POSIX, pthread_join .

+2

, .

. .

:

  • join.
  • event , .
  • Using the main thread to perform some processing currently performed by the workflow.
  • Periodically checking the boolean flag to decide whether to exit or not.

At the end of the day, it depends on your specific requirements.

+1
source

All Articles