managed to get ncurses to behave in threads

This commit is contained in:
2025-08-10 21:01:21 -06:00
parent 5595bf640e
commit bd7858cca0
+20 -41
View File
@@ -24,7 +24,7 @@
/* threading stuff */ /* threading stuff */
LIST *receivelist, *sendlist; LIST *receivelist, *sendlist;
pthread_t input_thread, send_thread, receive_thread, output_thread; pthread_t input_thread, send_thread, receive_thread, output_thread;
pthread_mutex_t sendmut, receivemut, sendcvmut, receivecvmut, chatmut, typemut; pthread_mutex_t sendmut, receivemut, sendcvmut, receivecvmut;
pthread_cond_t sendcv, receivecv; pthread_cond_t sendcv, receivecv;
char closed[] = "Connection closed."; char closed[] = "Connection closed.";
@@ -44,10 +44,9 @@ void *input(void *arg) {
/* Get a message from the user */ /* Get a message from the user */
msg = malloc(128); msg = malloc(128);
memset(msg, 0, 128); memset(msg, 0, 128);
pthread_mutex_lock(&typemut); mvwprintw(stdscr, row - 1, 0, "Type a message: ");
mvprintw(row - 1, 0, "Type a message: "); wgetstr(stdscr, msg);
getstr(msg); wrefresh(stdscr);
pthread_mutex_unlock(&typemut);
/* Add to sendlist */ /* Add to sendlist */
pthread_mutex_unlock(&sendmut); pthread_mutex_unlock(&sendmut);
@@ -58,23 +57,8 @@ void *input(void *arg) {
if (ListCount(sendlist) == 1) { if (ListCount(sendlist) == 1) {
pthread_cond_signal(&sendcv); pthread_cond_signal(&sendcv);
} }
/* NCURSES formatting stuff */
pthread_mutex_lock(&chatmut);
wmove(chatlog, 0, 0);
wdeleteln(chatlog);
wmove(chatlog, crow - 1, 0);
wdeleteln(chatlog);
wprintw(chatlog, "You: %s", msg);
redrawwin(chatlog);
wrefresh(chatlog);
pthread_mutex_unlock(&chatmut);
pthread_mutex_lock(&typemut);
mvprintw(row - 1, 0, "Type a message: ");
refresh();
pthread_mutex_unlock(&typemut);
} }
return 0; return 0;
} }
@@ -103,15 +87,23 @@ void *sender(void *arg) {
bytes = send(confd, msg, 128, 0); bytes = send(confd, msg, 128, 0);
/* Stop execution if the user closes the connection */ /* Stop execution if the user closes the connection */
if ((strncmp(msg, "/c", 2)) == 0) { if ((strncmp(msg, "/c", 2)) == 0 || bytes == -1) {
pthread_cancel(receive_thread); pthread_cancel(receive_thread);
pthread_cancel(input_thread); pthread_cancel(input_thread);
pthread_cancel(output_thread); pthread_cancel(output_thread);
pthread_exit(&closed); pthread_exit(&closed);
} }
move(row - 1, 0);
clrtoeol(); /* NCURSES formatting stuff */
mvprintw(row - 1, 0, "Type a message: "); /* First, update chat log */
wmove(chatlog, 0, 0);
wdeleteln(chatlog);
wmove(chatlog, crow, 0);
mvwprintw(chatlog, crow - 1, 0, "You: %s", msg);
wrefresh(chatlog);
/* Now, clear typed message on bottom line */
wmove(stdscr, row - 1, 16);
wclrtoeol(stdscr);
refresh(); refresh();
} }
return 0; return 0;
@@ -121,7 +113,7 @@ void *sender(void *arg) {
void *receiver(void *arg) { void *receiver(void *arg) {
char *buf; char *buf;
int bytes; int bytes;
char error[] = "pthread receiver exit";
while (1) { while (1) {
buf = malloc(128); buf = malloc(128);
memset(buf, 0, 128); memset(buf, 0, 128);
@@ -138,10 +130,6 @@ void *receiver(void *arg) {
ListAppend(receivelist, buf); ListAppend(receivelist, buf);
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
} }
else {
perror("recv");
pthread_exit(&error);
}
if (ListCount(receivelist) == 1) { if (ListCount(receivelist) == 1) {
pthread_cond_signal(&receivecv); pthread_cond_signal(&receivecv);
} }
@@ -165,15 +153,14 @@ void *output(void *arg) {
ListRemove(receivelist); ListRemove(receivelist);
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
pthread_mutex_lock(&chatmut);
wmove(chatlog, 0, 0); wmove(chatlog, 0, 0);
wdeleteln(chatlog); wdeleteln(chatlog);
wmove(chatlog, crow - 1, 0); wmove(chatlog, crow - 1, 0);
wdeleteln(chatlog); wdeleteln(chatlog);
mvwprintw(chatlog, crow - 1, 0, "%s: %s", s, msg); mvwprintw(chatlog, crow - 1, 0, "%s: %s", s, msg);
redrawwin(chatlog); wmove(stdscr, row - 1, 16);
wrefresh(chatlog); wrefresh(chatlog);
pthread_mutex_unlock(&chatmut); refresh();
} }
return 0; return 0;
@@ -220,14 +207,6 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Error: send cv mutex init failed\n"); fprintf(stderr, "Error: send cv mutex init failed\n");
return -1; return -1;
} }
if (pthread_mutex_init(&chatmut, NULL) != 0) {
fprintf(stderr, "Error: chat mutex init failed\n");
return -1;
}
if (pthread_mutex_init(&typemut, NULL) != 0) {
fprintf(stderr, "Error: type mutex init failed\n");
return -1;
}
if (pthread_cond_init(&sendcv, NULL) != 0) { if (pthread_cond_init(&sendcv, NULL) != 0) {
fprintf(stderr, "Error: send CV init failed\n"); fprintf(stderr, "Error: send CV init failed\n");
return -1; return -1;