[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
endwin() doesn't work in pthread cleanup function
From: |
Mike |
Subject: |
endwin() doesn't work in pthread cleanup function |
Date: |
Thu, 21 Sep 2006 15:06:27 +0800 |
User-agent: |
Thunderbird 1.5.0.2 (X11/20060501) |
The following piece of code can't reset the shell after it terminates.
Once endwin is moved to main(), it works.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <curses.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
void cleanup_win(void *data)
{
endwin();
return;
}
void *thread1(void *data)
{
char buf[256];
int old_state, i=0;
pthread_detach(pthread_self());
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
pthread_cleanup_push(cleanup_win, NULL);
(void) initscr(); /* Initialize the curses library. */
cbreak();
echo();
while (1) {
mvprintw(0, 0, "i=%d", i);
refresh();
sleep(1); /* Sleep 1 seconds. */
}
pthread_cleanup_pop(1);
pthread_setcancelstate(old_state, NULL);
return NULL;
}
sighandler_t old_sighandler;
int main(int argc, char *argv[])
{
pthread_t thread_id;
int c;
/* Ignore signal to avoid terminating thread1. */
old_sighandler = signal(SIGINT, SIG_IGN);
if (old_sighandler == SIG_ERR) {
perror("signal() error");
exit(1);
}
pthread_create(&thread_id, NULL, thread1, NULL);
while (1) {
c = getc(stdin); /* Press any key to terminate thread1. */
if (c != EOF) {
pthread_cancel(thread_id);
signal(SIGINT, old_sighandler);
break;
}
}
exit(0);
}
- endwin() doesn't work in pthread cleanup function,
Mike <=