Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2005
sleep


Message 1 in thread

I'm looking for some suggestions to create a 
portable sleep function .
I would like to avoid:
#ifdef __WIN32__
    sleep(int msec)
#else 
    usleep(int usec)

I was thinking of a single shot timer but don't see how
to get that in a function.  Any ideas?


Message 2 in thread

"Duane Hebert" <spoo@xxxxxxxxx> wrote in message
news:d33go5$o0j$1@xxxxxxxxxxxxxxxxxxxxx

> I was thinking of a single shot timer but don't see how
> to get that in a function.  Any ideas?

This seems to work:

void spoo(unsigned msec) {
    QWaitCondition W;
    W.wait(msec);
    return;
}

Anyone see a problem with this on either win32 or *nix?


Message 3 in thread

On April 7, 2005 09:36 am, Duane Hebert wrote:
> "Duane Hebert" <spoo@xxxxxxxxx> wrote in message
> news:d33go5$o0j$1@xxxxxxxxxxxxxxxxxxxxx
>
> > I was thinking of a single shot timer but don't see how
> > to get that in a function.  Any ideas?
>
> This seems to work:
>
> void spoo(unsigned msec) {
>     QWaitCondition W;
>     W.wait(msec);
>     return;
> }

I'm curious, why would you want to do this?


Message 4 in thread

"Christopher Thompson" <chris@xxxxxxxxxxxxxxxxx> wrote in message
news:200504110837.20360.chris@xxxxxxxxxxxxxxxxxxxx


> I'm curious, why would you want to do this?

I want to replace Sleep() on Windows with something
portable instead of using precompiler macros to
redefine it to usleep() or something on Linux and
possibly something else on Mac.


Message 5 in thread

On April 11, 2005 12:50 pm, Duane Hebert wrote:
> "Christopher Thompson" <chris@xxxxxxxxxxxxxxxxx> wrote in message
> news:200504110837.20360.chris@xxxxxxxxxxxxxxxxxxxx
>
> > I'm curious, why would you want to do this?
>
> I want to replace Sleep() on Windows with something
> portable instead of using precompiler macros to
> redefine it to usleep() or something on Linux and
> possibly something else on Mac.

Are you aware that it violates the event-driven programming pattern?

That's not to say there aren't ever good reasons to call sleep in an 
event-driven environment, just that you should understand the consequences.


Message 6 in thread

"Christopher Thompson" <chris@xxxxxxxxxxxxxxxxx> wrote in message
news:200504111255.28092.chris@xxxxxxxxxxxxxxxxxxxx
> On April 11, 2005 12:50 pm, Duane Hebert wrote:

> Are you aware that it violates the event-driven programming pattern?

Sure.

> That's not to say there aren't ever good reasons to call sleep in an
> event-driven environment, just that you should understand the
consequences.

We do a lot of hardware I/O stuff in our control systems.  In case of serial
transfer we have separate threads that handle the waits, timeouts etc.
For our profibus based I/O we don't need threads but when initializing
we need to sleep for a few msecs to allow the CP to do its setup.  Most
of this stuff happens before the GUI stuff is built.

Thanks for the input though.


Message 7 in thread

...try this.

#include <qthread.h>

class I : public QThread
{
public:
	static void sleep(unsigned long secs) {
		QThread::sleep(secs);
	}
	static void msleep(unsigned long msecs) {
		QThread::msleep(msecs);
	}
	static void usleep(unsigned long usecs) {
		QThread::usleep(usecs);
	}
};

...and somewhere in your code write this:

I::sleep(60); // ::msleep or :: usleep

...or something appropriate.


Message 8 in thread

<mbady@xxxxx> wrote in message news:425AD14B.4080209@xxxxxxxx
> ...try this.
> 
> #include <qthread.h>
> 
> class I : public QThread
> {
> public:
> static void sleep(unsigned long secs) {
> QThread::sleep(secs);
> }
> static void msleep(unsigned long msecs) {
> QThread::msleep(msecs);
> }
> static void usleep(unsigned long usecs) {
> QThread::usleep(usecs);
> }
> };
> 
> ...and somewhere in your code write this:
> 
> I::sleep(60); // ::msleep or :: usleep
> 
> ...or something appropriate.

Subclassing QThread and supplying a static sleep
was something that I thought about.  Thanks.


Message 9 in thread

On April 7, 2005 10:39, Duane Hebert wrote:
> I'm looking for some suggestions to create a
> portable sleep function .

Why not use QThread::sleep()?

-- 
 [ signature omitted ] 

Message 10 in thread

"Simon Perreault" <nomis80@xxxxxxxxxxx> wrote in message news:200504091121.09142.nomis80@xxxxxxxxxxxxxx
> On April 7, 2005 10:39, Duane Hebert wrote:
> > I'm looking for some suggestions to create a
> > portable sleep function .
>
> Why not use QThread::sleep()?

I'm not in a QThread.  I'm in the main thread.
Or am I missing something?
QWaitCondition seems to be working in
the main thread and in boost threads.  I'm trying
to assess its overhead but it looks OK.


Message 11 in thread

you can use qthread::sleep() in a non-threaded application to cause
the whole program to sleep

Mike

On Apr 9, 2005 6:31 PM, Duane Hebert <spoo@xxxxxxxxxx> wrote:
> "Simon Perreault" <nomis80@xxxxxxxxxxx> wrote in message news:200504091121.09142.nomis80@xxxxxxxxxxxxxx
> > On April 7, 2005 10:39, Duane Hebert wrote:
> > > I'm looking for some suggestions to create a
> > > portable sleep function .
> >
> > Why not use QThread::sleep()?
> 
> I'm not in a QThread.  I'm in the main thread.
> Or am I missing something?
> QWaitCondition seems to be working in
> the main thread and in boost threads.  I'm trying
> to assess its overhead but it looks OK.
> 
> --
> List archive and information: http://lists.trolltech.com/qt-interest/
> 


-- 
 [ signature omitted ] 

Message 12 in thread

"Mike Zupan" <hijinks@xxxxxxxxx> wrote in message
news:7227c6c7050409165018e9f12e@xxxxxxxxxxxxxxxxx
> you can use qthread::sleep() in a non-threaded application to cause
> the whole program to sleep

sleep() is a protected member.  How do I call it static like you suggest?

\MSVC\Projects\Common\useful.cpp(16) : error C2248: 'QThread::sleep' :
cannot access protected member declared in class 'QThread'