diff --git a/src/main.c b/src/main.c
index 8df667cc1b1ec86a4627217d01db6497207c36b5..745c762ca9d451a2a82ec7bd40e8025cbbb551e8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,7 +5,7 @@
 \*/
 /**/
 #include "global.h"
-RCSID("$Id: main.c,v 1.84 2000/03/28 08:10:53 hubbe Exp $");
+RCSID("$Id: main.c,v 1.85 2000/03/30 04:39:17 hubbe Exp $");
 #include "fdlib.h"
 #include "backend.h"
 #include "module.h"
@@ -618,6 +618,17 @@ void low_exit_main(void)
   {
     INT32 num,size,recount=0;
     fprintf(stderr,"Exited normally, counting bytes.\n");
+
+#ifdef _REENTRANT
+    if(count_pike_threads()>1)
+    {
+      fprintf(stderr,"Byte counting aborted, because all threads have not exited properly.\n");
+      verbose_debug_exit=0;
+      return;
+    }
+#endif
+
+
     search_all_memheaders_for_references();
 
     count_memory_in_arrays(&num, &size);
diff --git a/src/threads.c b/src/threads.c
index 95478064122313cc2d10b1d30ea29dcc93f5c5d3..7ced3896940a704fd394ef08cd4876aee751ce81 100644
--- a/src/threads.c
+++ b/src/threads.c
@@ -1,5 +1,5 @@
 #include "global.h"
-RCSID("$Id: threads.c,v 1.115 2000/03/25 20:58:30 hubbe Exp $");
+RCSID("$Id: threads.c,v 1.116 2000/03/30 04:39:17 hubbe Exp $");
 
 int num_threads = 1;
 int threads_disabled = 0;
@@ -378,6 +378,7 @@ void exit_interleave_mutex(IMUTEX_T *im)
 #define THREAD_TABLE_SIZE 127  /* Totally arbitrary prime */
 
 static struct thread_state *thread_table_chains[THREAD_TABLE_SIZE];
+static int num_pike_threads=0;
 
 void thread_table_init(void)
 {
@@ -421,6 +422,7 @@ void thread_table_insert(struct object *o)
 /*  dumpmem("thread_table_insert",&s->id, sizeof(THREAD_T)); */
 #endif
   mt_lock( & thread_table_lock );
+  num_pike_threads++;
   if((s->hashlink = thread_table_chains[h]) != NULL)
     s->hashlink->backlink = &s->hashlink;
   thread_table_chains[h] = s;
@@ -433,6 +435,7 @@ void thread_table_delete(struct object *o)
   struct thread_state *s = OBJ2THREAD(o);
 /*  dumpmem("thread_table_delete",&s->id, sizeof(THREAD_T)); */
   mt_lock( & thread_table_lock );
+  num_pike_threads--;
   if(s->hashlink != NULL)
     s->hashlink->backlink = s->backlink;
   *(s->backlink) = s->hashlink;
@@ -519,6 +522,10 @@ void f_all_threads(INT32 args)
   f_aggregate(sp-oldsp);
 }
 
+int count_pike_threads(void)
+{
+  return num_pike_threads;
+}
 
 static void check_threads(struct callback *cb, void *arg, void * arg2)
 {
diff --git a/src/threads.h b/src/threads.h
index e9452508330bf6cf2e0ecb4b4ae3b2cf39cf21ff..0c73c00f01fcf17fe3bc242f8c71ae9f4cd79e50 100644
--- a/src/threads.h
+++ b/src/threads.h
@@ -1,5 +1,5 @@
 /*
- * $Id: threads.h,v 1.77 2000/03/29 22:08:50 hubbe Exp $
+ * $Id: threads.h,v 1.78 2000/03/30 04:39:17 hubbe Exp $
  */
 #ifndef THREADS_H
 #define THREADS_H
@@ -556,6 +556,7 @@ void thread_table_delete(struct object *o);
 struct thread_state *thread_state_for_id(THREAD_T tid);
 struct object *thread_for_id(THREAD_T tid);
 void f_all_threads(INT32 args);
+int count_pike_threads(void);
 TH_RETURN_TYPE new_thread_func(void * data);
 void f_thread_create(INT32 args);
 void f_thread_set_concurrency(INT32 args);