Qt 多线程的一个例子

(1)用VC6.0新建个Win32 Console Application工程

(2)Project Settings里面Link标签页面添加qtmain.lib qt-mt320.lib
   Project Settings里面C/C++标签页面添加QT_THREAD_SUPPORT

(3)源代码文件(main.cpp):

#include <qthread.h>

class MyThread : public QThread
{    
public:
    virtual void run();
   
};

void MyThread::run()
{
    for( int count = 0; count < 20; count++ )
    {
        sleep( 1 );
        qDebug( "Ping!" );
    }
}

int main()
{
    MyThread a;
    MyThread b;
    a.start();
    b.start();
    a.wait();
    b.wait();
}
注释:
This will start two threads, each of which writes Ping! 20 times to the screen and exits.
The wait() calls at the end of main() are necessary because exiting main() ends the program,
unceremoniously killing all other threads.
Each MyThread stops executing when it reaches the end of MyThread::run(),
just as an application does when it leaves main(). 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值