Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 04-18-2006, 12:09 PM   #1
Registered: Apr 2005
Distribution: Debian
Posts: 154

Rep: Reputation: 30
Problems with pthread in ncurses

I am creating a program in ncurses that displays a list of songs on the screen. The user selects which song he wants and hits enter to play it. This program is piping to mpg321 in order to play the song. I create a thread to view the output of mpg321 and wait for the song to end so the program can go to the next song.

The problem is that the thread cannot directly manipulate the screen for some reason. This is the relevant code for the thread:

for (;
read(stat_pipe[0], tempbuff, 1);
while (tempbuff[0] != '\n')
statbuff[z]= tempbuff[0];
statbuff[z+1]= '\0';
tempbuff[0]= '\0';
if (z > 49) break;
read(stat_pipe[0], tempbuff, 1);
if ((strcmp(statbuff, "@P 0")) == 0) /*Song has ended*/
menu_driver(my_menu, REQ_DOWN_ITEM);
cur_item= current_item(my_menu);

The description of the item is the name of the song which is then piped to mpg321. The problem is that the selector on the item doesn't move down on the screen. However, the next song is played (unless you're on the last song, but that is expected behavior). My second idea was to inform the main thread in some way that a song has ended, so the main thread can do the menu work, but the main thread is blocked at getch() waiting for user input.
Old 04-18-2006, 08:09 PM   #2
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Wow... are there really people in the world who program in good old ncurses? This is typically one of those last little things which cost you a week or so to solve.

Now I have written multithreaded programs which used ncurses, but I always had only one thread accessing the screen. And it is quite some time ago that I wrote any c code at all, but let's try:

Apparently the current item is updated. You request it, and use it to select the next song, correct? That means that the menu itself is handled correctly, but that the screen update is postponed for some reason.

Have you tried to issue an explicit window refresh?

So to see you are using the menu without an associated window. Have you tried to associate a window with the menu and issue an explicit refresh?

To debug, you could start gdb in a different terminal, and attach to the main thread, and set a breakpoint in the screen refresh routine of ncurses. The second thread would just continue to run, but your program would stop in the debugger when the breakpoint is hit. You then could see if the refresh function is not called at all, or refuses to refresh for some reason.

If this all fails, you could make getch() non-blocking in the main loop in your main program. Then you could use a variable to signal when a track is finished, and advance the menu pointer. But then, you would not need the multi-threaded approach at all.

That would be a pity because this multi-threaded approach is quite elegant.

Old 04-19-2006, 02:13 AM   #3
Registered: Apr 2005
Distribution: Debian
Posts: 154

Original Poster
Rep: Reputation: 30
There is only one window in the app, stdscr. I have tried an explicit refresh, but no dice. Here's another odd thing I didn't notice before: say it has moved down three songs in the list since the user hits enter. The cursor is still over the original song. However, once the cursor is moved up or down, it will jump to the actual position of the cursor with one space moved up or down, respectively.

The idea of having getch be nonblocking sounds good to me, and it would remove the extra complexity of the thread. I just need to figure out how to make getch nonblocking, but I can find out real quick. The only real reason for using the thread was because of a precursor to the program I'm making, which implemented a thread.

This is sounding more like a bug in ncurses than a bug in my program. Doing a little more playing around, it seems like ncurses has problems with multiple threads. I moved the part of the program that getches into the thread and the part seen above in the main thread. Strangely enough, getch couldn't read from any of the keys EXCEPT for the function keys (F1, F2....). I'm thinking I may want to complile this on a FreeBSD machine I have, since FreeBSD handles threads differently than does Linux.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with pthread aru_04 Linux - General 1 11-12-2004 07:01 AM
ncurses compile-time problems jclark7777 Programming 2 07-17-2004 09:12 AM
pthread problems and parse errors Stabby Red Hat 1 01-28-2004 11:56 PM
pthread and ncurses bretzeltux Programming 4 10-30-2003 04:07 PM
ncurses-5.2-28 conflicts with file from package ncurses-5.2-12 tubby Linux - Software 4 06-16-2002 01:00 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration