]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/worm.c
ncurses 5.6 - patch 20080112
[ncurses.git] / test / worm.c
index 534e626e23b8b8e945201ae8edf1bcb4dd7f91e2..172b1dd846f7c681123140d469e1c5534a22adb1 100644 (file)
@@ -61,7 +61,7 @@ Options:
   traces will be dumped.  The program stops and waits for one character of
   input at the beginning and end of the interval.
 
   traces will be dumped.  The program stops and waits for one character of
   input at the beginning and end of the interval.
 
-  $Id: worm.c,v 1.49 2007/09/29 17:35:57 tom Exp $
+  $Id: worm.c,v 1.51 2008/01/13 01:03:23 tom Exp $
 */
 
 #include <test.priv.h>
 */
 
 #include <test.priv.h>
@@ -93,6 +93,9 @@ typedef struct worm {
 #endif
 } WORM;
 
 #endif
 } WORM;
 
+static unsigned long sequence = 0;
+static bool quitting = FALSE;
+
 static WORM worm[40];
 static short **refs;
 
 static WORM worm[40];
 static short **refs;
 
@@ -313,12 +316,22 @@ use_window(WINDOW *win, int (*func) (WINDOW *, void *), void *data)
 #endif
 
 #ifdef USE_PTHREADS
 #endif
 
 #ifdef USE_PTHREADS
+static bool
+quit_worm(void)
+{
+    napms(10);                 /* let the other thread(s) have a chance */
+    return quitting;
+}
+
 static void *
 start_worm(void *arg)
 {
 static void *
 start_worm(void *arg)
 {
-    for (;;) {
-       napms(20);
-       use_window(stdscr, draw_worm, arg);
+    unsigned long compare = 0;
+    while (!quit_worm()) {
+       while (compare < sequence) {
+           ++compare;
+           use_window(stdscr, draw_worm, arg);
+       }
     }
     return NULL;
 }
     }
     return NULL;
 }
@@ -484,13 +497,13 @@ main(int argc, char *argv[])
            }
        }
     }
            }
        }
     }
-    napms(10);
     refresh();
     nodelay(stdscr, TRUE);
 
     while (!done) {
        int ch;
 
     refresh();
     nodelay(stdscr, TRUE);
 
     while (!done) {
        int ch;
 
+       ++sequence;
        if ((ch = getch()) > 0) {
 #ifdef TRACE
            if (trace_start || trace_end) {
        if ((ch = getch()) > 0) {
 #ifdef TRACE
            if (trace_start || trace_end) {
@@ -533,6 +546,7 @@ main(int argc, char *argv[])
             * normal operation -T.Dickey
             */
            if (ch == 'q') {
             * normal operation -T.Dickey
             */
            if (ch == 'q') {
+               quitting = TRUE;
                done = TRUE;
                continue;
            } else if (ch == 's') {
                done = TRUE;
                continue;
            } else if (ch == 's') {
@@ -557,6 +571,14 @@ main(int argc, char *argv[])
        free(w->xpos);
        free(w->ypos);
     }
        free(w->xpos);
        free(w->ypos);
     }
+#endif
+#ifdef USE_PTHREADS
+    /*
+     * Do this just in case one of the threads did not really exit.
+     */
+    for (n = 0; n < number; n++) {
+       pthread_join(worm[n].thread, NULL);
+    }
 #endif
     ExitProgram(EXIT_SUCCESS);
 }
 #endif
     ExitProgram(EXIT_SUCCESS);
 }