Results 1 to 8 of 8

Thread: does main thread has sleep() ?

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default does main thread has sleep() ?

    I know we can not put main thread in sleep, But I have tried for sleep method i did not find it & I found it in QThread only.
    I can understand that as we should not put main thread in sleep that may be the reason we don't have sleep for main thread.
    But is sleep() available to put main thread ideal for a while ?? // I struggled a lot to find sleep but could not
    Thanks :-)

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    The sleep() methods are public static.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    Quote Originally Posted by anda_skoa View Post
    The sleep() methods are public static.

    Cheers,
    _
    No they are protected methods.
    Thanks :-)

  4. #4
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    They are public in Qt 5.x and protected in Qt 4.x. You could use something like the following:
    Qt Code:
    1. class ThreadSleeper:public QThread
    2. {
    3. private:
    4. ThreadSleeper(){};
    5. virtual ~ThreadSleeper(){};
    6. public:
    7. using QThread::msleep;
    8. };
    9.  
    10. ...
    11.  
    12. ThreadSleeper::msleep(msec);
    To copy to clipboard, switch view to plain text mode 

    Best regards
    ars
    Last edited by ars; 11th October 2015 at 14:21. Reason: spelling corrections

  5. #5
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    Quote Originally Posted by ars View Post
    They are public in Qt 5.x and protected in Qt 4.x. You could use something like the following
    ars
    I wanted to sleep in main thread. Like below

    Main()
    {
    ///some standard code

    sleep(1000);

    }

    And I am unsing Qt4.8
    Thanks :-)

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    Quote Originally Posted by prasad_N View Post
    I wanted to sleep in main thread.
    And I am unsing Qt4.8
    Given there are only 10 minutes between your comment and the answer by ars, I assume you have read it by now.

    Cheers,
    _

  7. #7
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    Could have tried it before replying. It worked.
    Thanks.
    Last edited by prasad_N; 11th October 2015 at 15:46.
    Thanks :-)

  8. #8
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: does main thread has sleep() ?

    Sorry but can I use threadsleeper::msleep(m_sleep); to sleep main thread ?
    You can use it to sleep any thread. More precisely, it sleeps the thread that calls this method. And as the main thread is a thread as any other thread, why shouldn't it be sent to sleep by calling this method? If you don't believe, just try this little code snippet:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QThread>
    3. #include <iostream>
    4. #include <QTime>
    5.  
    6. class ThreadSleeper:public QThread
    7. {
    8. private:
    9. ThreadSleeper(){};
    10. virtual ~ThreadSleeper(){};
    11.  
    12. public:
    13. using QThread::msleep;
    14. };
    15.  
    16.  
    17. int main(int , char *[])
    18. {
    19. std::cout << "sleeping for 5s" << std::endl;
    20. QTime t;
    21. t.start();
    22. ThreadSleeper::msleep(5000);
    23. std::cout << "slept for " << t.elapsed() << "ms" << std::endl;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Best regards
    ars

    @Prasad_N: I didn't see your previous reply before posting the above.

  9. The following user says thank you to ars for this useful post:

    prasad_N (11th October 2015)

Similar Threads

  1. Replies: 1
    Last Post: 28th March 2012, 19:58
  2. Replies: 5
    Last Post: 22nd February 2011, 22:21
  3. Replies: 9
    Last Post: 28th November 2009, 21:31
  4. Replies: 16
    Last Post: 7th October 2009, 09:17
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 23:09

Tags for this Thread

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.