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
Re: does main thread has sleep() ?
The sleep() methods are public static.
Cheers,
_
Re: does main thread has sleep() ?
Quote:
Originally Posted by
anda_skoa
The sleep() methods are public static.
Cheers,
_
No they are protected methods.
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:
Code:
{
private:
ThreadSleeper(){};
virtual ~ThreadSleeper(){};
public:
};
...
ThreadSleeper::msleep(msec);
Best regards
ars
Re: does main thread has sleep() ?
Quote:
Originally Posted by
ars
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
Re: does main thread has sleep() ?
Quote:
Originally Posted by
prasad_N
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,
_
Re: does main thread has sleep() ?
Could have tried it before replying. It worked.
Thanks.
Re: does main thread has sleep() ?
Quote:
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:
Code:
#include <QCoreApplication>
#include <QThread>
#include <iostream>
#include <QTime>
{
private:
ThreadSleeper(){};
virtual ~ThreadSleeper(){};
public:
};
int main(int , char *[])
{
std::cout << "sleeping for 5s" << std::endl;
t.start();
ThreadSleeper::msleep(5000);
std::cout << "slept for " << t.elapsed() << "ms" << std::endl;
}
Best regards
ars
@Prasad_N: I didn't see your previous reply before posting the above.