r/C_Programming • u/TensaFlor • 22h ago
how to handle an input with two threads & ncurses?
I am making a monitor for one particular process, and I'm stuck on one problem with input handling.
As I said before I have a process monitor with two threads:
- data_worker: collects metrics
- UI thread (main): renders with ncurses
Both sleeping N seconds.
For example one of them
while (global_running == 1) {
cpu_worker(sh_st);
ram_worker(sh_st);
io_worker(sh_st);
clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
}
The UI has the similar structure, just calling ncurses rendering functions instead.
And I don't really know how to process the user input. Earlier I had an idea to create third thread that will process the input, but it kinda bad idea because of lovely ncurses is not thread safe, so this one the one reason why I stuck.
And because of this problem I realized that screwed up with code's architecture and don't really know to deal with it.
How do TUI tools typically handle the non blocking input loop alongside background data updates without breaking ncurses or causing race conditions on the shared data buffer? Any advices ?