博客
关于我
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 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的 SSL 模块安装
查看>>
Nginx 的优化思路,并解析网站防盗链
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 相关介绍(Nginx是什么?能干嘛?)
查看>>
nginx 禁止以ip形式访问服务器
查看>>
NGINX 端口负载均衡
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
Nginx 负载均衡配置详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置dist 加上跨域配置
查看>>
nginx 配置https(一)—— 自签名证书
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx 配置服务器文件上传与下载
查看>>
Nginx 配置清单(一篇够用)
查看>>
Nginx 配置解析:从基础到高级应用指南
查看>>