LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Exiting program w/ pthreads from sig handler (https://www.linuxquestions.org/questions/programming-9/exiting-program-w-pthreads-from-sig-handler-207580/)

LogicG8 07-20-2004 05:41 PM

Exiting program w/ pthreads from sig handler
 
I wrote a long running server process that uses pthreads
It runs with 2 threads of execution
1) Waits for data from a device and sends data to clients
2) Waits for new clients to connect

Both threads run in an "infinite" loop servicing clients until a signal is recieved

Basic execution flows like this:

Gather resources call atexit with cleanup functions
Start thread to accept clients (blocks in accept(2))
Go into infinite loop waiting for data (blocks in poll(2))

... continues along till signal recieved

The signal handler basically just calls exit

/* This is where I lose exactly what's going on */
the functions specified to atexit run.

I haven't found any good example code. I am not sure if I am still running as
if in a signal handler or what's happening with the thread. I haven't had any
failures but that could be a coincidence.

What I was thinking might be the Right Thing
is to call setjmp in main
run normally (ie wait for signal to terminate)
longjmp from the sig handler
grab a lock on the shared data
call pthread_cancel
and then return from main
let the cleanup functions run

What is a clean way to terminate the program?

Chris Parker 07-29-2004 05:56 PM

Why not just fork like most web servers? Much easier to code for.


All times are GMT -5. The time now is 09:18 AM.