2010-08-03 34 views
0

我正在使用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); 

    } 

+0

爲什麼在使用select時使用線程?不要過分複雜的事情! – mvds 2010-08-04 01:27:20

+0

你是對的還是錯的。選擇阻止UI在檢查數據時工作的塊。至少在我的測試中。 但是,我過了複雜的事情,最終解決了問題 – 2010-08-04 05:44:36

回答

0

可能爲時已晚,對於這個問題的答案。

您沒有指定要控制光標的窗口。我假設你想將光標放在chat_box或chat_window的(height + 1,curx)位置上。

可以在stdout窗口中使用移動功能控制光標,該窗口是原始終端,但不是在您創建的窗口(chat_box和chat_window)上。用於在用戶創建的窗口中控制光標的功能是wmove。

int move(int y,int x);

int wmove(WINDOW * win,int y,int x);

希望這可以幫助其他人面臨類似的問題。如果這不是正確的解決方案,請注意。