Qt使用多线程编译项目的方法

编译新项目发现编译速度很慢,看了下电脑的配置,10代的i5处理器,6核心12线程的处理能力,在编译时却仅用了10%的cpu占用,只使用了一个线程进行编译,很明显没有有效的cpu。对于这种情况应该开启make的多线程编译模式

点击项目,构建和运行,构建步骤里面。点击make后面的详情,在make参数里面加上-j 12这个参数,意思是使用12个线程编译程序
在这里插入图片描述
之后编译速度大大加快了,cpu使用率也达到了90%。对于性能强大的台式cpu,就是要榨干它的性能。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt使用多线程调用FFmpeg进行解码需要注意以下几个步骤: 1. 在Qt项目中添加FFmpeg库,可以使用CMake或者手动编译安装。 2. 创建一个继承于QThread类的线程类,并在run()函数中实现FFmpeg解码逻辑。 3. 在主线程中创建多个线程实例,并启动线程。 下面是一个简单的示例代码: ```cpp // FFmpeg解码线程类 class DecodeThread : public QThread { public: DecodeThread(const QString &filename, QObject *parent = nullptr) : QThread(parent), m_filename(filename) { } protected: void run() override { AVFormatContext *formatCtx = nullptr; AVCodecContext *codecCtx = nullptr; AVCodec *codec = nullptr; AVPacket *packet = nullptr; AVFrame *frame = nullptr; // 初始化FFmpeg av_register_all(); avcodec_register_all(); // 打开文件 if (avformat_open_input(&formatCtx, m_filename.toStdString().c_str(), nullptr, nullptr) < 0) { emit errorOccurred("Failed to open file"); return; } // 获取流信息 if (avformat_find_stream_info(formatCtx, nullptr) < 0) { emit errorOccurred("Failed to find stream information"); avformat_close_input(&formatCtx); return; } // 查找视频流索引 int videoStreamIndex = -1; for (unsigned int i = 0; i < formatCtx->nb_streams; i++) { if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { emit errorOccurred("Failed to find video stream"); avformat_close_input(&formatCtx); return; } // 获取视频解码器 codec = avcodec_find_decoder(formatCtx->streams[videoStreamIndex]->codecpar->codec_id); if (!codec) { emit errorOccurred("Failed to find codec"); avformat_close_input(&formatCtx); return; } // 初始化解码器上下文 codecCtx = avcodec_alloc_context3(codec); if (!codecCtx) { emit errorOccurred("Failed to allocate codec context"); avformat_close_input(&formatCtx); return; } if (avcodec_parameters_to_context(codecCtx, formatCtx->streams[videoStreamIndex]->codecpar) < 0) { emit errorOccurred("Failed to copy codec parameters to codec context"); avcodec_free_context(&codecCtx); avformat_close_input(&formatCtx); return; } if (avcodec_open2(codecCtx, codec, nullptr) < 0) { emit errorOccurred("Failed to open codec"); avcodec_free_context(&codecCtx); avformat_close_input(&formatCtx); return; } // 解码视频帧 packet = av_packet_alloc(); frame = av_frame_alloc(); while (av_read_frame(formatCtx, packet) >= 0) { if (packet->stream_index == videoStreamIndex) { int ret = avcodec_send_packet(codecCtx, packet); if (ret < 0) { emit errorOccurred("Failed to send packet to codec context"); break; } while (true) { ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { emit errorOccurred("Error occurred while decoding frame"); break; } // 处理解码后的视频帧 // ... av_frame_unref(frame); } } av_packet_unref(packet); } av_packet_free(&packet); av_frame_free(&frame); avcodec_free_context(&codecCtx); avformat_close_input(&formatCtx); } private: QString m_filename; }; // 在主线程中创建并启动多个解码线程 QStringList filenames = { "video1.mp4", "video2.mp4", "video3.mp4" }; QList<DecodeThread*> threads; for (const auto &filename : filenames) { DecodeThread *thread = new DecodeThread(filename); connect(thread, &DecodeThread::errorOccurred, [=](const QString &error) { qDebug() << "Error occurred:" << error; }); thread->start(); threads.append(thread); } // 等待所有线程结束 for (const auto &thread : threads) thread->wait(); // 释放线程资源 for (const auto &thread : threads) delete thread; ``` 注意:在FFmpeg中,每个解码器上下文只能在一个线程中使用,因此不同线程之间的解码器上下文是独立的。另外,在解码时需要注意线程安全问题,例如对于AVPacket和AVFrame结构体的操作应该在临界区内进行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百口可乐__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值