#include <QtCore/QCoreApplication>
#include <QThread>
#include <stdio.h>
class ThreadPrinter:public QThread
{
public:
ThreadPrinter(char str);
void stop();
private:
bool flgRun;
char strPrint;
protected:
void run();
};
ThreadPrinter::ThreadPrinter(char str)
{
flgRun = true;
strPrint = str;
}
void ThreadPrinter::stop()
{
flgRun = false;
}
void ThreadPrinter::run()
{
while(flgRun)
{
printf("%c\n",strPrint);
sleep(1);
}
printf("Thread %c exit!\n",strPrint);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ThreadPrinter threadA('A');
ThreadPrinter threadB('B');
threadA.start();
threadB.start();
while(1)
{
if(getchar()=='B')
{
if(threadA.isRunning())
{
threadA.stop();
threadA.wait();
}
if(threadB.isRunning())
{
threadB.stop();
threadB.wait();
}
}
}
return a.exec();
}
Qt多线程编程(1)——多线程演示
于 2014-03-20 21:50:48 首次发布