简体   繁体   中英

how to output text to console to specific coordinates without moving the cursor coordinates in c++

I have a c++ application, compiling in DEV C++ and it includes the pthread multithreading library. I have 2 threads. The default int main() , and a thread I created, that will print out the days of the week, one at a time, every few seconds. But when I execute this, it works, but there is only one cursor in the program. This is where the problem is: I need the way to write these days of the week to the screen to the coordinates 0, 50 for example, just positioning the text not the cursor. This is so the first thread with the cursor, doesn't interfere with my second thread output and they will both execute that the same time

Assuming you are using ncurses or something like that? So your console terminal is a shared resource, you have to protect it with a mutex and take care for the cursor positioning in each thread. You could also declare one thread the boss which has its cursor-position restored by the other thread. That other thread then would, after aquiring the mutex, do something like getcurx, getcury, do its own positioning and outout, and then restore the cursor pos retrieved with getcurx/y.
note that in each case, all threads need to aquire the mutex before accessing the terminal.
You can also check wether you can come along with the builtin thread support in ncurses, look out for use_screen , use_window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM