bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problems and documentation for concurrent ncurses


From: Paul Emberson
Subject: Problems and documentation for concurrent ncurses
Date: Sat, 27 Aug 2005 00:51:40 +0100
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050729)

Hi,

I have some software using threads with ncurses which worked on some
machines but not others.  After some experimenting, I discovered that
there were only problems on dual core machines.

My first question is, is there any documentation I can use for ncurses
concurrency issues?

Secondly, I have now managed to solve the problem (details below) but
can anybody give me an explanation so I don't run into future problems?

I have a controller running in one thread polling for a key press with a
call to getch() and data being displayed in another thread which calls
refresh().  On regular single processor core machines, everything worked.

On a dual core machine, I have to put locks around the calls to getch()
and refresh(), otherwise the display is corrupted and crashes if the
window is resized.  Simplified source below.

Thanks,

Paul

#include <pthread.h>
#include <ncurses.h>

void * display_func(void * args);

bool done = false;

pthread_mutex_t ncurses_lock = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char **argv)
{
     pthread_t display_thread;
     int c;

     initscr();
     cbreak();
     noecho();
     nodelay(stdscr, TRUE);
     refresh();

     pthread_create(&display_thread,
                    NULL,
                    display_func,
                    NULL);
     while (!done)
     {
         pthread_mutex_lock(&ncurses_lock);
         c = getch();
         pthread_mutex_unlock(&ncurses_lock);

         if (c == 'Q')
         {
             done = true;
         }
     }

     pthread_join(display_thread, NULL);
     endwin();
     return 0;
}

void * display_func(void * args)
{
     while (!done)
     {
         mvaddnstr(10, 10, "hello", 5);
         pthread_mutex_lock(&ncurses_lock);
         refresh();
         pthread_mutex_unlock(&ncurses_lock);
         usleep(250000);
     }

     return (void*)0;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]