Index: /trunk/Ohana/src/opihi/include/pantasks.h
===================================================================
--- /trunk/Ohana/src/opihi/include/pantasks.h	(revision 41482)
+++ /trunk/Ohana/src/opihi/include/pantasks.h	(revision 41483)
@@ -265,7 +265,10 @@
 int  DeleteClient (int client);
 void *ListenClients (void *data);
+void QuitClientThread (void);
 
 // functions related to the server threads
 void *CheckJobsAndTasksThread (void *data);
+void QuitJobsAndTasksThread (void);
+void QuitControllerThread (void);
 
 void CheckTasksSetState (int state);
Index: /trunk/Ohana/src/opihi/lib.shell/gprint.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/gprint.c	(revision 41482)
+++ /trunk/Ohana/src/opihi/lib.shell/gprint.c	(revision 41483)
@@ -304,4 +304,5 @@
 
   if (stream[0].mode == GP_FILE) {
+    // fprintf (stderr, "printing to %s, mode FILE, thread %d\n", stream[0].name, (int) stream[0].thread);
     status = vfprintf (stream[0].file, format, argp);
     fflush (stream[0].file);
@@ -310,4 +311,5 @@
     }
   } else {
+    // fprintf (stderr, "printing to %s, mode BUFF, thread %d\n", stream[0].name, (int) stream[0].thread);
     vPrintIOBuffer (stream[0].buffer, format, argp);
   }
Index: /trunk/Ohana/src/opihi/pantasks/CheckTasks.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/CheckTasks.c	(revision 41482)
+++ /trunk/Ohana/src/opihi/pantasks/CheckTasks.c	(revision 41483)
@@ -52,5 +52,5 @@
     // XXX this should be optional
     fuzz = task[0].exec_period*(0.5*drand48() - 0.25);
-    task[0].last.tv_usec = 1e6*(fuzz - (int)fuzz);
+    task[0].last.tv_usec += 1e6*(fuzz - (int)fuzz);
     task[0].last.tv_sec += (int) fuzz;
 
Index: /trunk/Ohana/src/opihi/pantasks/ListenClients.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/ListenClients.c	(revision 41482)
+++ /trunk/Ohana/src/opihi/pantasks/ListenClients.c	(revision 41483)
@@ -7,4 +7,10 @@
 static int *clients;
 static IOBuffer **buffers;
+
+static int ClientThreadRuns = TRUE;
+
+void QuitClientThread (void) {
+  ClientThreadRuns = FALSE;
+}
 
 void InitClients () {
@@ -83,5 +89,5 @@
   gprintSetBuffer (GP_ERR);
 
-  while (1) {
+  while (ClientThreadRuns) {
 
     /* Wait up to 0.5 second - need to timeout to update client list */
@@ -174,4 +180,5 @@
     /* check if we need to shut down the thread */
   }
+  return NULL;
 }
 
Index: /trunk/Ohana/src/opihi/pantasks/controller_threads.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/controller_threads.c	(revision 41482)
+++ /trunk/Ohana/src/opihi/pantasks/controller_threads.c	(revision 41483)
@@ -5,4 +5,6 @@
 static int CheckControllerRun = FALSE;
 
+static int ControllerThreadRuns = TRUE;
+
 void CheckControllerSetState (int state) {
   CheckControllerRun = state;
@@ -10,4 +12,8 @@
 int CheckControllerGetState () {
   return (CheckControllerRun);
+}
+
+void QuitControllerThread (void) {
+  ControllerThreadRuns = FALSE;
 }
 
@@ -26,5 +32,5 @@
   }
 
-  while (1) {
+  while (ControllerThreadRuns) {
 
     // check for thread suspend
@@ -47,3 +53,4 @@
     usleep (500000); // allow other threads a chance to run
   }
+  return NULL;
 }
Index: /trunk/Ohana/src/opihi/pantasks/jobs_and_tasks_thread.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/jobs_and_tasks_thread.c	(revision 41482)
+++ /trunk/Ohana/src/opihi/pantasks/jobs_and_tasks_thread.c	(revision 41483)
@@ -5,4 +5,6 @@
 static int CheckTasksRun = FALSE;
 static int CheckJobsRun = FALSE;
+
+static int JobsAndTasksThreadRuns = TRUE;
 
 void CheckJobsSetState (int state) {
@@ -23,4 +25,8 @@
 }
 
+void QuitJobsAndTasksThread (void) {
+  JobsAndTasksThreadRuns = FALSE;  
+}
+
 void *CheckJobsAndTasksThread (void *data) {
   OHANA_UNUSED_PARAM(data);
@@ -39,5 +45,5 @@
   }
 
-  while (1) {
+  while (JobsAndTasksThreadRuns) {
 
     // one run of the task checker
@@ -63,4 +69,5 @@
     }      
   }
+  return NULL;
 }
 
Index: /trunk/Ohana/src/opihi/pantasks/pantasks.c.in
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/pantasks.c.in	(revision 41482)
+++ /trunk/Ohana/src/opihi/pantasks/pantasks.c.in	(revision 41483)
@@ -7,4 +7,7 @@
 # define opihi_rcfile ".pantasksrc"
 
+static pthread_t JobsAndTasksThread;
+static pthread_t controllerThread;
+
 /* program-dependent initialization */
 void program_init (int *argc, char **argv) {
@@ -12,7 +15,4 @@
   OHANA_UNUSED_PARAM(argv);
   
-  pthread_t JobsAndTasksThread;
-  pthread_t controllerThread;
-
   auto_break = TRUE;
 
@@ -67,7 +67,12 @@
 /* add program-dependent exit functions here */
 void cleanup () {
+  // stop the threads before calling the free functions
   QuitKapa ();
   QuitController ();
   ConfigFree ();
+
+  // tell the threads to exit and wait for them to exit
+  QuitJobsAndTasksThread(); pthread_join (JobsAndTasksThread, NULL);
+  QuitControllerThread(); pthread_join (controllerThread, NULL);
 
   FreeBasic ();
Index: /trunk/Ohana/src/opihi/pantasks/pantasks_server.c.in
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/pantasks_server.c.in	(revision 41482)
+++ /trunk/Ohana/src/opihi/pantasks/pantasks_server.c.in	(revision 41483)
@@ -15,4 +15,8 @@
 void special_init (int *argc, char **argv);
 
+static pthread_t JobsAndTasksThread;
+static pthread_t controllerThread;
+static pthread_t clientsThread;
+
 /* program-dependent initialization */
 int main (int argc, char **argv) {
@@ -20,7 +24,4 @@
   char hostname[256], portinfo[256];
   char log_stdout[1024], log_stderr[1024], tmpname[1024];
-  pthread_t JobsAndTasksThread;
-  pthread_t clientsThread;
-  pthread_t controllerThread;
   int InitSocket, BindSocket;
   SockAddress Address;
@@ -136,4 +137,9 @@
   QuitController ();
   ConfigFree ();
+
+  QuitJobsAndTasksThread(); pthread_join (JobsAndTasksThread, NULL);
+  QuitControllerThread();   pthread_join (controllerThread, NULL);
+  QuitClientThread();       pthread_join (clientsThread, NULL);
+
   InitBasic ();
   InitData ();
