使用多线程手动写出循环打印ABABABAB...
2017-12-27 14:52
239 查看
平时没在意,敲敲代码就出来了,可实际用笔在纸上书写的时候就不知所措了
QT比较熟悉,就用它来实现吧!
#ifndef HELLOTHREAD_H
#define HELLOTHREAD_H
#include <QThread>
#include <QMutex>
class HelloThread : public QThread
{
public:
HelloThread(int type=0);
void stop();
protected:
void run();
public:
int m_type;
int m_exitflag;
private:
static QMutex m_mutex;
static int m_status;
};
#endif // HELLOTHREAD_H
主要是用全局变量实现线程间的通讯,为了保证全局变量的状态唯一,需要加锁控制,就用最简单的QMutex吧~~~~
#include <QCoreApplication>
#include "hellothread.h"
//实现多线程,循环打印ABABABABABAB......
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("in main thread...%p\n", QThread::currentThreadId() );
HelloThread TA(0);
HelloThread TB(1);
TA.start();//开启线程
TB.start();
if( 'q'==getchar() )//等待用户输入
{
TA.quit();//退出线程(不管用)
TB.quit();
printf("in main exiting...\n");
TA.stop();//使用条件退出
TB.stop();
}
TA.wait();//等待线程结束
TB.wait();
printf("in main exited thread...%p\n", QThread::currentThreadId());
exit(0);//按键才有效,为何?
return a.exec();
}
貌似达到了目的,提笔就忘字,怎么办啊!!!
QT比较熟悉,就用它来实现吧!
#ifndef HELLOTHREAD_H
#define HELLOTHREAD_H
#include <QThread>
#include <QMutex>
class HelloThread : public QThread
{
public:
HelloThread(int type=0);
void stop();
protected:
void run();
public:
int m_type;
int m_exitflag;
private:
static QMutex m_mutex;
static int m_status;
};
#endif // HELLOTHREAD_H
#include "hellothread.h" //静态全局变量 QMutex HelloThread::m_mutex; int HelloThread::m_status = 0; HelloThread::HelloThread(int type) : m_type(type) { m_exitflag = false; } void HelloThread::stop() { m_exitflag = true; } void HelloThread::run() { printf("\n enter thread = %p\n", QThread::currentThreadId() ); while( !m_exitflag ) { QMutexLocker locker(&HelloThread::m_mutex); if(0==this->m_type && HelloThread::m_status==0) { printf("A\n"); HelloThread::m_status = 1;//this->m_status也是可以的,但不规范 } else if(1==this->m_type && HelloThread::m_status==1) { printf("B\n"); HelloThread::m_status = 0;//this->m_status也是可以的,但不规范 } locker.unlock(); msleep(10); } m_exitflag = false; printf("\n leave thread = %p\n", QThread::currentThreadId() ); }
主要是用全局变量实现线程间的通讯,为了保证全局变量的状态唯一,需要加锁控制,就用最简单的QMutex吧~~~~
#include <QCoreApplication>
#include "hellothread.h"
//实现多线程,循环打印ABABABABABAB......
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("in main thread...%p\n", QThread::currentThreadId() );
HelloThread TA(0);
HelloThread TB(1);
TA.start();//开启线程
TB.start();
if( 'q'==getchar() )//等待用户输入
{
TA.quit();//退出线程(不管用)
TB.quit();
printf("in main exiting...\n");
TA.stop();//使用条件退出
TB.stop();
}
TA.wait();//等待线程结束
TB.wait();
printf("in main exited thread...%p\n", QThread::currentThreadId());
exit(0);//按键才有效,为何?
return a.exec();
}
貌似达到了目的,提笔就忘字,怎么办啊!!!
相关文章推荐
- 使用LODOP实现循环打印
- java中使用二重循环打印图形
- java基础_使用循环实现打印a-z
- java.不使用for,while循环打印99乘法表
- C#不允许使用循环语句、条件语句,在控制台中打印出1-1000这1000个数
- 如何使用一个循环打印九九乘法表
- 多线程循环打印ABC
- 请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。更不能写1000个printf
- Java多线程循环打印ABC 10次,可能是东半球最美解决方案了
- 有三个线程ID分别是ABC,用多线程编程在屏幕上循环打印ABCABC....十次
- 【C语言】把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。
- 3D 打印的模型 告别手动添加支撑的方式 — 使用 Meshmixer 软件
- 使用map来进行票数统计工作,循环输入多个人名,作为key存储到map中,对应的value就是该人获得的票数(即重复输入的次数),当重复输入时,需要对相应的数据进行修改。最红输quit结束循环,打印
- Java 简单算法--打印乘法口诀(只使用一次循环)
- 10、使用双重循环打印图形
- 如何使用双循环打印九九乘法表
- 小代码 项目使用 文字逐渐打印 循环打印 一字一字地展现 javascript js
- 不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串
- Java实现多线程经典问题:使用三个线程实现输出ABCABC循环
- Java多线程 循环打印ABC 10次