Results 1 to 14 of 14

Thread: QThread and QProcess problem

  1. #1
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QThread and QProcess problem

    I've make a player with a playlist. For playing songs i'm using "play" which i start in a QProcess which runs inside the QThread. The code looks like this:
    Qt Code:
    1. jBoxPlayer::jBoxPlayer(QListBox *&playlist)
    2. {
    3. _pPlayList = playlist;
    4. _pPlayer = new QProcess(0,"jPlayer");
    5. }
    6.  
    7. jBoxPlayer::~jBoxPlayer()
    8. {
    9. _pPlayList=NULL;
    10. delete _pPlayer;
    11. }
    12.  
    13. void jBoxPlayer::run()
    14. {
    15. QLBItemExt *lb;
    16. int i=0;
    17.  
    18. while (1)
    19. {
    20. // qDebug(QString("loop"));
    21. if ((_pPlayList->count()==0) || _pPlayer->isRunning()) continue;
    22. // if (_pPlayList->count()>0) {QString("if Playlist.");}
    23. // if ( _pPlayer->isRunning()) continue;
    24.  
    25.  
    26. qDebug("Playing new song.");
    27. _pPlayer->clearArguments();
    28. lb = (QLBItemExt *)_pPlayList->item(i++);
    29. _pPlayer->addArgument("play");
    30. qDebug(QString("Playing: %1").arg(lb->GetPath()));
    31. _pPlayer->addArgument(lb->GetPath());
    32. lb=NULL;
    33. // mutex.lock();
    34. // _pPlayList->removeItem(0);
    35. // mutex.unlock();
    36. _pPlayer->start();
    37. //_bPlaying=true;
    38. qDebug("loop end.");
    39. }
    40. qDebug("i'm out");
    41. }
    To copy to clipboard, switch view to plain text mode 

    the playlist is populated whit 2 songs. When i run the app the player plays well the first song. but when finished playing it throw an segmentation fault. The problem is when i uncoment the line 20,22,23 and comment line 21 it works fine - whitout the segmentation fault.
    i have no experience with QThreads and QProcess and i don't know if the while(1) is the right approach (the signal processExited() it should be a better way i think) for the player to play untill the end of the playlist.

    Any help on pointing me to the right direction would be great. or how in the real world threads and process are handled

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QProcess problem

    Well, you have to consider a few things. Like, what if the user presses stop? Then you need to stop the thread.

    i have no experience with QThreads and QProcess and i don't know if the while(1) is the right approach (the signal processExited() it should be a better way i think) for the player to play untill the end of the playlist.
    For beginning, you should add a flag in the thread( a boolean member) and a setter for it.
    Instead of the infinite loop, you should test that flag. You set it to false when the user presses stop.

    As for the loop, it is better to put the thread to sleep if it has nothing to do, instead of "continue".

    Regarding the playlist:
    First note: do not pass a widget, instead pass a list containing the items in the list.
    Widgets should be handled only by the GUI thread. Even if you don't modify it, it's still bad practice.
    The list should be a pointer in the GUI thread. This structure should get updated if the user adds/removes something from the playlist. Use a mutex to synchronize the threads on that structure( The gui writes, and the player reads the list).

    How does it exactly work? You remove items as they are finished playing?
    You get a segmentation fault because you're trying to access a list item that does not exist.
    That is done in the 28 of the code you posted.

    Instead of the tests you do already, why don't you test if "i" is greater than the number of items in the list. If it is, then sleep( you played them all ). Otherwise, play the item at position i.


    Regards

  3. #3
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and QProcess problem

    How does it exactly work? You remove items as they are finished playing?
    You get a segmentation fault because you're trying to access a list item that does not exist.
    That is done in the 28 of the code you posted.
    first idea was to delet each song that was plaied. The seg. fault was throwen before the line 26 (the message has not been written to stdout)

    First note: do not pass a widget, instead pass a list containing the items in the list.
    Should i use the QMap or some c++ countainers? Do i have to override the update function and compare the contest of "mylist" and the playlist?

    put the thread to sleep - you mean for n seconds or is there a way for wake the thread up?

    at the end.. thank's for showing me the approach

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QProcess problem

    first idea was to delet each song that was plaied. The seg. fault was throwen before the line 26 (the message has not been written to stdout)
    it doesn't make any sense segfaulting at that location since yoiu basically do the same thing. BTW, what's with "QString("if...")" ?

    Should i use the QMap or some c++ countainers? Do i have to override the update function and compare the contest of "mylist" and the playlist?
    You can use whatever structure seems fit to you. A QMap is OK, as long as you update it to reflect the widget's content.

    put the thread to sleep - you mean for n seconds or is there a way for wake the thread up?
    No, you just call QThread::msleep in case the thread has no work to do( i.e. no songs in playlist ). You currently continue the loop. If you use this strategy and look at the processor utilization while no songs in list, then you will see it will be at 100%.

    Regards

  5. #5
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and QProcess problem

    it doesn't make any sense segfaulting at that location since yoiu basically do the same thing. BTW, what's with "QString("if...")" ?
    I have no answer for that. QString("") shoul be qDebug("") my mistake...
    the problem with segfault is a past... the sleeping thread was successful.

    for now my code look like that:
    Qt Code:
    1. while(playlist>0) // in the future will be a QMap or a FIFO list
    2. {
    3. if (player->isRunning())
    4. {
    5. sleep(n);
    6. continue;
    7. }
    8. // do something with the song while mutex is locked
    9. }
    To copy to clipboard, switch view to plain text mode 
    till now i don't have any problem.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QProcess problem

    ok then.

    Regards

  7. #7
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and QProcess problem

    i was reading the background of how to implement the communication between queue and the playlist...
    i thought to code like that
    Qt Code:
    1. update()
    2. {
    3. // do the stuff with playlist and the queue
    4. update();
    5. }
    To copy to clipboard, switch view to plain text mode 
    or am i missing something?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QProcess problem

    Of who's update are you talking about? The list box?

    I was thinking more like: you have a list of songs( QList, whatever). This should behave like an external model for the list box. That is when you add an item, you update the list, and as a result of this the widget will get updated also.

    You should have a few functions for accessing the list's contents.
    Reading and writing to the list should be guarded with a mutex.

    You also could add a remove item function which you *call* from the thread when the current song ends. By call I mean emitting a signal from the thread.

    But I guess there are other methods. You should choose the one that feels more scalable to you, since I assume your application is just at its beginning, and it will get more complex.

    Regards

  9. #9
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and QProcess problem

    I was talking about the update of the main widget but i haven't thought about the signal and slot way of communication...
    i haven't worked with that (excluding the tutor of course) so i'll try it... something new to experiment. it sounds interesting.

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QProcess problem

    This is the way it is recommended to be done in Qt4. Should work in 3 also.

    BTW, why don't you switch to v4? It has a lot of new things...Can't even be compared to 3.

    Regards

  11. #11
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and QProcess problem

    i really don't know. i haven't read the specification for the minimum HW that it works on??
    i have a 900 MHz Duron and 512 MB RAM
    at my job i have a 3GHz and 1GB RAM and working with vs2005 and it is slow!

  12. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QProcess problem

    Quote Originally Posted by codebehind View Post
    i really don't know. i haven't read the specification for the minimum HW that it works on??
    i have a 900 MHz Duron and 512 MB RAM
    at my job i have a 3GHz and 1GB RAM and working with vs2005 and it is slow!
    Slow in what way? When you compile Qt4 apps? I think it can't get better than that.
    My last Qt project( at work ), was taking about 15-20 minutes to compile, on a similar computer( 3ghz, 2gb mem). But it was pretty big.

    Ultimately your application's requirements dictate the resources to use.
    Qt4 is in many ways faster than 3.


    Regards

  13. #13
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and QProcess problem

    slow in the way of working with it! a few open windows and let say two studios and explorer and IBrowser (Firefox), task manager and a DB Tool (AQT) and MicrosoftExchange... and it needs some time to change between app. That i mean for slow. compile time is fast in front of switching betwin app.

    anyway i started to download Qt4, will see!

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread and QProcess problem

    Why separate thread? Usually there is no need to execute an external process in a separate thread because it doesn't block anyhow.
    J-P Nurmi

Similar Threads

  1. Replies: 4
    Last Post: 27th July 2006, 12:13
  2. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.