• 11826阅读
  • 6回复

QThread::sleep()为什么要设成保护的 [复制链接]

上一主题 下一主题
离线rashka
 

只看楼主 倒序阅读 楼主  发表于: 2009-07-10
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
真想不通为什么QT一定要将QThread里的sleep函数设成保护的,这样在其他非QThread的子类里都没法用了,但我这个类已经继承了一个QObject的子类,不可能再继承QThread了。但我为了平台无关性又不能用linux下sleep函数。请问有什么解决方法吗?
在线snow_man_0

只看该作者 1楼 发表于: 2009-07-10
class QMyThread : public QThread
{
     mySleep()
     {
        sleep();
     }
}
试下,看行不行
离线sbtree
只看该作者 2楼 发表于: 2009-07-10
sleep是一个静态保护成员函数,不是还有一个weit公有成员函数吗?可以试一试
windows 7 + VC++2008 + Qt4.5.2
离线rashka

只看该作者 3楼 发表于: 2009-07-10
wait函数是用来等待其他线程结束的,无法让当前线程阻塞
离线sbtree
只看该作者 4楼 发表于: 2009-07-10
引用第3楼rashka于2009-07-10 18:43发表的  :
wait函数是用来等待其他线程结束的,无法让当前线程阻塞

建议你仔细看一下文档吧
bool QThread::wait ( unsigned long time = ULONG_MAX )

Blocks the thread until either of these conditions is met:

-The thread associated with this QThread object has finished execution (i.e. when it returns from run()). This function will return true if the thread has finished. It also returns true if the thread has not been started yet.
- time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the thread must return from run()). This function will return false if the wait timed out.
This provides similar functionality to the POSIX pthread_join() function.

See also sleep() and terminate().
windows 7 + VC++2008 + Qt4.5.2
离线rashka

只看该作者 5楼 发表于: 2009-07-10
我显然是看过文档了才这么说的,我还写程序测试过了:
#include <QtCore/QCoreApplication>
#include <QThread>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    printf ("wait 1000ms\n");
    QThread::currentThread()->wait(1000);
    printf ("finished\n");
    return a.exec();
}

输出为:
wait 1000ms
QThread::wait: Thread tried to wait on itself
finished
离线sbtree
只看该作者 6楼 发表于: 2009-07-10
你这样测试不太合理,个人觉得。我的帖子里面有我测试的过程,你可以参照,不过我测试的结果显示了另外的问题,当另外讨论了。
http://www.qtcn.org/bbs/read.php?tid=20025
windows 7 + VC++2008 + Qt4.5.2
快速回复
限100 字节
 
上一个 下一个