我正在使用ncurses在C中編寫基於文本的客戶端。程序的主循環直到檢測到按鍵,然後處理它並繼續等待另一按鍵。需要關於ncurses遊標和線程的幫助
我有一個線程啓動(在下面發佈),阻塞(使用select)等待來自服務器的輸入,當它接收到它時,將它添加到聊天日誌緩衝區並將緩衝區打印到屏幕上。它完美的作品。
我知道ncurses不是線程安全的,但我對線程的理解是,只要我100%確定只有一個線程一次調用ncurses,它就能正常工作。
我的問題是與光標位置。
它是用move(height+1, curx);
行修改的,無論我傳遞給它什麼值,ncurses似乎完全忽略了這個調用,並將我的光標置於不同的位置。我似乎無法影響它。
爲了進一步解釋問題,在我的主線程(按鍵循環)中,我使用了相同的互斥鎖。當光標在這些代碼段中更新時,它按計劃運行。當它從下面的接收線程更新時,遊標調用將被忽略。
任何想法?
receive thread
char buf[512];
fd_set read_fds;
FD_ZERO(&read_fds);
int nbytes;
for (;;) {
read_fds = master;
select(sockfd+1, &read_fds, NULL, NULL, NULL);
pthread_mutex_lock(&mutexdisplay);
memset(&buf, 0, sizeof buf);
nbytes = recv(sockfd, buf, 512, 0);
buf[nbytes] = 0;
add_chatmsg(chatlog, &numchatlog, buf);
// erase window
werase(chat_window);
// redraw border
wborder(chat_window, '|', '|', '-', '-', '+', '+', '+', '+');
// scroll completely into the future
chatlogstart = numchatlog-1;
// print the chat log
print_chatlog(chatlog, &numchatlog, &chatlogstart, &height);
move(height+1, curx);
// refresh window
wrefresh(chat_box);
wrefresh(chat_window);
pthread_mutex_unlock(&mutexdisplay);
}
爲什麼在使用select時使用線程?不要過分複雜的事情! – mvds 2010-08-04 01:27:20
你是對的還是錯的。選擇阻止UI在檢查數據時工作的塊。至少在我的測試中。 但是,我過了複雜的事情,最終解決了問題 – 2010-08-04 05:44:36