Re: Terminate threads

Michael Abbott <[email protected]> Thu, 20 Oct 2011 09:46:51 -0700 (PDT)
Newsgroups gmane.comp.lib.state-threads.user
Message-ID <[email protected]>
> I have a process A that creates a thread to do logging, then forks
> another process B.

Programs using State Threads should fork before creating threads.  One
idiom is to have a master process which forks and uses no threads
itself, while the child processes manage their own threads.

> How do I get B to terminate any existing inherited threads such as the
> logging thread.

State Threads does not provide a way to "kill" a thread.  Instead each
thread can check a flag in its main loop to see whether it should exit.
The flag can be a global variable or a pointer passed as the arg to
st_thread_create().  For threads which are blocking,
st_thread_interrupt() wakes them up but does not kill them.

Your "IAmSpartacus" example is close but I'd code it this way:

int run_logger = 1;
st_thread_t logger_thread = st_thread_create(..., &run_logger, ...);
...
if (fork() == 0) {
  run_logger = 0;
  st_thread_interrupt(logger_thread);
}
...
static void *flush_acclog_buffer(int *keep_running)
{
  while (*keep_running) {
    if (st_sleep(ACCLOG_FLUSH_INTERVAL) == 0)
      logbuf_flush();
  }
  return NULL;
}

------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Ciosco Self-Assessment and learn 
about Cisco certifications, training, and career opportunities. 
http://p.sf.net/sfu/cisco-dev2dev