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

Qt-interest Archive, January 2007
No public msleep/usleep?


Message 1 in thread

Why are the msleep and usleep static methods of QThread declared as 
protected?  In some code that I write with Qt, especially in Qt 4 where 
you can use QCoreApplication, I need to introduce a small delay (for 
example in serial port communications) but the code isn't in a QThread 
so I ended up writing my own little msleep function that calls Sleep on 
Windows and usleep on Linux.  Ideally I would have liked to use 
something like Qthread::currentThread()->msleep(100) but I can't since 
msleep in QThread is protected.

--
 [ signature omitted ] 

Message 2 in thread

Brad Pepers wrote:
> Why are the msleep and usleep static methods of QThread declared as 
> protected?  In some code that I write with Qt, especially in Qt 4 where 
> you can use QCoreApplication, I need to introduce a small delay (for 
> example in serial port communications) but the code isn't in a QThread 
> so I ended up writing my own little msleep function that calls Sleep on 
> Windows and usleep on Linux.  Ideally I would have liked to use 
> something like Qthread::currentThread()->msleep(100) but I can't since 
> msleep in QThread is protected.
> 

class Helper: public QThread {
public:
	static void msleep(int ms)
	{
		QThread::msleep(ms);
	}
};

does the trick.

Not nice, but certainly better than providing own implementations.

/eno

--
 [ signature omitted ] 

Message 3 in thread

<troll@xxxxxxxxxxxx> wrote in message news:459EA527.6040206@xxxxxxxxxxxxxxx
> Brad Pepers wrote:
> > Why are the msleep and usleep static methods of QThread declared as
> > protected?  In some code that I write with Qt, especially in Qt 4 where
> > you can use QCoreApplication, I need to introduce a small delay (for
> > example in serial port communications) but the code isn't in a QThread
> > so I ended up writing my own little msleep function that calls Sleep on
> > Windows and usleep on Linux.  Ideally I would have liked to use
> > something like Qthread::currentThread()->msleep(100) but I can't since
> > msleep in QThread is protected.
> >
>
> class Helper: public QThread {
> public:
> static void msleep(int ms)
> {
> QThread::msleep(ms);
> }
> };
>
> does the trick.
>
> Not nice, but certainly better than providing own implementations.

void sleep(unsigned long msecs) {
  QWaitCondition w;
  QMutex sleepmutex;
  sleepmutex.lock();
  w.wait(&sleepmutex, msecs);
  sleepmutex.unlock();
}

Seems to work though it's probably not the most
efficient.  We mostly use boost threads
so I didn't like the idea of having
two thread classes in the same project.

--
 [ signature omitted ] 

Message 4 in thread

troll@xxxxxxxxxxxx wrote:
> Brad Pepers wrote:
>> Why are the msleep and usleep static methods of QThread declared as 
>> protected?  In some code that I write with Qt, especially in Qt 4 
>> where you can use QCoreApplication, I need to introduce a small delay 
>> (for example in serial port communications) but the code isn't in a 
>> QThread so I ended up writing my own little msleep function that calls 
>> Sleep on Windows and usleep on Linux.  Ideally I would have liked to 
>> use something like Qthread::currentThread()->msleep(100) but I can't 
>> since msleep in QThread is protected.
>>
> 
> class Helper: public QThread {
> public:
>     static void msleep(int ms)
>     {
>         QThread::msleep(ms);
>     }
> };
> 
> does the trick.
> 
> Not nice, but certainly better than providing own implementations.

True but I was worried there was some reason that QThread didn't make 
the methods public.  If there isn't, I'll just do that but it seems an 
ugly thing when the methods should just be public.

--
 [ signature omitted ] 

Message 5 in thread

Hi,

> [...]
> Windows and usleep on Linux.  Ideally I would have liked to use 
> something like Qthread::currentThread()->msleep(100) but I can't since 
> msleep in QThread is protected.

You wouldn't be do that anyway since they are static methods.

--
 [ signature omitted ] 

Message 6 in thread

I wrote:
>> [...]
>> Windows and usleep on Linux.  Ideally I would have liked to use 
>> something like Qthread::currentThread()->msleep(100) but I can't since 
>> msleep in QThread is protected.
> 
> You wouldn't be do that anyway since they are static methods.

Sorry, you could do that of course:
	Qthread::currentThread()->msleep(100)
which would be equivalent to:
	Qthread::msleep(100)
but the following:
	getSomeOtherthread()->msleep(100)
wouldn't do what expected.

I guess this is why it was decided to make the function protected, to 
prevent some users from shooting themselves in the foot, although it may 
also annoy other users who do know what they're doing.

--
 [ signature omitted ] 

Message 7 in thread

I use the following:

//-----------------------------------------------------------------
// This class let's us sleep in seconds, milleseconds, and microseconds
class sotoSleep : 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);
		}
};

Then I can do this:

sotoSleep::sleep(1);       // for seconds
sotoSleep::msleep(1000);   // for milliseconds
sotoSleep::sleep(1000000); // for microseconds.

Pepsiman

-----Original Message-----
From: Brad Pepers [mailto:brad@xxxxxxxxxxxxxxx] 
Sent: Friday, January 05, 2007 1:00 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: No public msleep/usleep?

Why are the msleep and usleep static methods of QThread declared as 
protected?  In some code that I write with Qt, especially in Qt 4 where 
you can use QCoreApplication, I need to introduce a small delay (for 
example in serial port communications) but the code isn't in a QThread 
so I ended up writing my own little msleep function that calls Sleep on 
Windows and usleep on Linux.  Ideally I would have liked to use 
something like Qthread::currentThread()->msleep(100) but I can't since 
msleep in QThread is protected.

--
 [ signature omitted ]