cmultithreadingpthreadscursespdcurses

Why mvprintw works only after getch?


mvprintw/mvaddch (pdcurses) print text only after getch() (even if it is not connected in any way and the getch is in detach thread).

A question from a different angle: How can i use mvprintw() in a detach thread without getch() for loop print? (A small rendering engine or something like that) //EDIT

// MAIN WHILE() (Strong simplification for debug)

do
    {
        //printf(" [%d] ", exit_flag);
        char player_action = getch();
        
        //render_player_info();
        
        //printf("%d ", player_x);
        mvprintw(1, player_x, "%d ", player_y);
        mvaddch(1, player_y, 'C');
        
        //Sleep(1);
    }
    while(exit_flag == 0);
// CODE BLOCK FROM LAUNCH FUNC (Main while() start after thread detach)

int res;
    
    pthread_t thread_render_engine;
    
    void *thread_func_render_engine(void * arg) 
    {
        int count = 0;
        
        do
        {
            count++;
            printf(" F%d ", count);
            mvprintw(count/10 + 1, count, "P");
            mvaddch(count/10 + 2, count, 'A');
            
            render_player_info();
            
            Sleep(1000);
        } while (!exit_flag);
        
        pthread_exit(NULL);
    }
    
    res = pthread_create (&thread_render_engine, NULL, thread_func_render_engine, NULL);
    
    if (res != 0) {
        mvprintw(29, 0, "main error: can't create thread, status = %d\n", res);
        exit(-10);
    }

    res = pthread_detach(thread_render_engine);
    
    if (res != 0) {
        mvprintw(29, 0, "main error: can't detach thread, status = %d\n", res);
        exit(-11);
    }

I tried to delete getch form amin dowhile and default printf working without getch, but not mvprintw/mvaddch


Solution

  • After you've called functions like mvprintw() and mvaddch() to modify stdscr, you then call refresh() to update the display (getch() does this as a side-effect).

    curses doesn't update the window immediately because this way the number of cursor movement operations sent over a (possibly slow) terminal connection is minimised.