博客
关于我
Qt安全使用线程
阅读量:240 次
发布时间:2019-03-01

本文共 653 字,大约阅读时间需要 2 分钟。

       使用背景:项目中需要开线程去播放声音、视频,简单的线程可以实现,Qt中简单的线程就是继承QThread类,然后重新run()方法即可,但是new出来的对象要时刻记得释放,不然就会有崩溃问题。于是想办法使用更为安全的线程,让系统替自己去管理new的对象,就是使用QRunnable类结合线程池实现。

       初始方法:

.h

class PlayThread : public QThread{public:    void run();};

.cpp

void PlayThread::run(){    my_exec("aplay test.wav");    this->deleteLater();}

      更改后得方法:

.h

class playThread : public QRunnable{public:    playThread(){}    explicit playThread(QString music){this->music = music;}    void run();private:    QString music {"test.wav"};}

.cpp

void playThread::run(){    my_exec(("aplay "+this->music));}

      使用方法和之前一样,包含头文件,在使用的类,私有成员中声明一个QThreadPool *playpool;然后再cpp中调用:playpool->start(new playThread);即可。

 

转载地址:http://vret.baihongyu.com/

你可能感兴趣的文章
Nginx学习总结(3)——Nginx配置及应用场景之高级配置
查看>>
Nginx学习总结(4)——负载均衡session会话保持方法
查看>>
Nginx学习总结(5)——Nginx基本配置备忘
查看>>
Nginx学习总结(6)——Nginx + https + 免费SSL证书配置指南
查看>>
Nginx学习总结(7)——Nginx配置HTTPS 服务器
查看>>
Nginx学习总结(8)——Nginx服务器详解
查看>>
Nginx学习总结(9)——前端跨域问题解决
查看>>
nginx学习笔记
查看>>
nginx学习笔记001---Nginx的启动、停止与重启
查看>>
nginx学习笔记002---Nginx代理配置_案例1_实现了对前端代码的方向代理_并且配置了后端api接口的访问地址
查看>>
nginx学习笔记003---Nginx代理配置_注意,在Windows中路径要用/
查看>>
Nginx学习笔记(一) Nginx架构
查看>>
nginx学习路线
查看>>
Nginx安装
查看>>
Nginx安装SSL模块 nginx: the “ssl” parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx
查看>>
nginx安装stream模块配置tcp/udp端口转发
查看>>
nginx安装Stream模块配置tcp/udp端口转发
查看>>
Nginx安装与常见命令
查看>>
nginx安装与配置
查看>>
【Flink】Flink 2023 Flink 到 Doris 实时写入实践
查看>>