Results 1 to 4 of 4

Thread: Threads problem

  1. #1
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Threads problem

    Hi all,
    I am new to Threads!! I have a class derived from QWidget and it runs a thread
    which prints out messages. I use start, sleep, resume and terminate button to
    controle the thread. I dont know how to controle `sleep and resume` of thread.
    Here is the piece of code I have written,

    ThreadWidget::ThreadWidget(QWidget *parent )
    :QWidget(parent)
    {

    thinThread = new ThinThread();

    QPushButton *startButton= new QPushButton("start",this);
    QPushButton *sleepButton= new QPushButton("sleep",this);
    QPushButton *resumeButton= new QPushButton("resume",this);
    QPushButton *terminateButton = new QPushButton("terminate",this);

    connect (startButton,SIGNAL (clicked()), thinThread, SLOT(start()));
    connect (terminateButton,SIGNAL (clicked()), thinThread, SLOT(terminate()));
    //connect (resumeButton,SIGNAL (clicked()), ??;
    //connect (sleepButton,SIGNAL (clicked()), ??;

    QHBoxLayout *mainLayout = new QHBoxLayout(this);

    mainLayout->addWidget(startButton);
    mainLayout->addWidget(sleepButton);
    mainLayout->addWidget(resumeButton);
    mainLayout->addWidget(terminateButton);

    setLayout(mainLayout);

    }

    //ThinThread is derived from QThread!!

    void ThinThread ::run ()
    {
    while(1)
    {
    sleep(1);
    cout <<" in the thread\n";
    }

    }

    Can some body explain how to do it?

    Thanks in advance,
    Boss

  2. #2
    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: Threads problem

    Quote Originally Posted by boss_bhat
    Can some body explain how to do it?
    Start of by reading Thread Support in Qt. Then, see Qt's threading examples at /examples/threads.

    A few hints:
    • if you really want it to enter to an infinite loop inside run(), you'll need wait conditions
    • another (and maybe easier) option would be to enter to an event loop instead
    J-P Nurmi

  3. #3
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Threads problem

    I did something like this, it works fine but is this the right way of doing ??

    Qt Code:
    1. void ThinThread ::run ()
    2. {
    3.  
    4. label:
    5.  
    6. while(suspFlag)
    7. {
    8. sleep(1);
    9. cout <<" in the thread "<< endl;
    10. }
    11. if (!suspFlag)
    12. {
    13. while (!suspFlag)
    14. {
    15. sleep(1);
    16. }
    17. }
    18.  
    19. goto label;
    20.  
    21. cout <<"done with the thread\n";
    22. }
    23.  
    24. void ThinThread::suspend()
    25. {
    26. suspFlag=false;
    27. }
    28. void ThinThread::resume()
    29. {
    30. suspFlag=true;
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    please suggest me on this
    Thanks in advance,
    Boss
    Last edited by jacek; 2nd August 2006 at 15:30. Reason: added code tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads problem

    Quote Originally Posted by boss_bhat
    I did something like this, it works fine but is this the right way of doing ??
    Better use QWaitCondition.

    And please use the [ code ] tags.

Similar Threads

  1. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  2. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  3. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Problems with threads and windows
    By SkripT in forum Qt Programming
    Replies: 15
    Last Post: 16th January 2006, 17:46

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.