Chinnu
Chinnu

Reputation: 11

How to run multiple ncurses window using pthread

I am trying to simply run two ncurse windows using pthread. The code i have written is as follows:

#include<stdio.h>
#include<pthread.h>
#include<ncurses.h>
#include<sys/ioctl.h>
#include<string.h>
#include<unistd.h> 

 struct winsize w;
 WINDOW *win1,*win2;
 void createWin1(void){
       while(1){

                char  buffer[1024];
                win1=newwin(0,0,40,50);      
                box(win1,0,0); 
                wrefresh(win1); 
                sleep(1);
            }
}     
void createWin2(void){
          while(1){
                   win2=newwin(40,50,40,60);
                   box(win2,0,0);
                   wrefresh(win2);
                   sleep(1);
                  }
    }
void main()
  {    initscr();
       noecho();
       cbreak();
       start_color();
       use_default_colors();
       init_pair(1,COLOR_WHITE, -1);              
       pthread_t p1,p2;       
       pthread_create(&p1,NULL,(void *)createWin1,NULL);    
       pthread_create(&p2,NULL,(void *)createWin2,NULL);
       pthread_join(p1,NULL);
       pthread_join(p2,NULL);

}

Now the problem is I cant run two windows in parallel .This will show unexpected output. Can anyone please help me to find out the issue in my code.

Upvotes: 1

Views: 1188

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54525

The approach used in the sample code cannot work reliably because curses uses static/global variables. You can either setup mutexes around the ncurses calls (to ensure that input or output from one thread is separate from the others), or compile (there are few packages) the version with rudimentary threading support as a starting point.

In the ncurses FAQ, start with Why does (fill in the blank) happen when I use two threads?

Upvotes: 1

HDJEMAI
HDJEMAI

Reputation: 9800

i think you have to include #include ncurses.h in your source code ?

the declaration struct winsize w; seems to create an incomplete type

the variable flag is not declared in the scope of the function createWin1()

try to paste the entire code if possible

i've an error of including panel.h

i've a problem with my opensuse right now, i found some people with the same problem so i'm looking for that, i mean about including panel.h

error 'row' was not declared in this scope in win1=newwin(w.ws_row-row

I'm trying to find the paramiters you used for the variable w "ws_row-row" is it correct, try to search the content of WINDOW struct ??

i think you have to define newwin correctely with good values

WINDOW * win = newwin(nlines, ncols, y0, x0); http://hughm.cs.ukzn.ac.za/~murrellh/os/notes/ncurses.html#window

still four errors in my side

main has to return a value , i declared it int main, and i return zero, the compiler complain about that

there is some errors about conversion in pthread_create

you function have to be declared void* createWin1(void*) and not void createWin1(void)

do you have any compilation errors in your side or not ?

i'm trying to help you i'm not a specialist about ncurses

now the compilation is ok but i have linker errors undefined reference to newwin .....

it's a library problem, think we are not far from: https://github.com/mariostg/nffm/issues/2

i compiled with g++ -pthread test.c for now, i w'll look again tomorow

Upvotes: 0

Related Questions