QT中关线程的问题
2015-08-11 14:23
423 查看
一、前段时间写了个多线程的代码,当关线程的时候遇到当发送了关线程后,线程不能及时关掉,然后,影响后面的其他的线程的通信。
之前的办法如下:
void MyThread::stop()
{
mutex.lock;
t_stop = true;
mutex.unlock;
mcondition.wakeall;
}
void MyThread::run()
{
forever {
mutex.lock;
if (t_stop) {
t_stop = false;
mutex.unlock;
break;
}
mcondition.wait(&mutex, 15s);//延时15s
mutex.unlock;
dosomething();//会耗时大概10s种;uart和其他模块交互
}
}
更改后解决办法如下:
1、我的硬件平台是arm9
2、子线程里,重新实现了run函数;
t_stop 初始化为false;
大概是这样的:
void MyThread::stop()
{
mutex.lock;
t_stop = true;
mutex.unlock;
mcondition.wakeall;
}
void MyThread::run()
{
forever {
mutex.lock;
if (t_stop) {
t_stop = false;
mutex.unlock;
break;
}
mcondition.wait(&mutex, 15s);//延时15s
mutex.unlock;
mutex.lock;
if (t_stop) {
t_stop = false;
mutex.unlock;
break;
}
dosomething();//会耗时大概10s种;uart和其他模块交互
mutex.unlock;
}
}
3、然后主线程里面先打开,然后过段时间再关闭线程。
关闭:t->stop(); //MyThread t;
就如上所示的关闭线程这样正确吗?
二、这里使用了条件变量。
这样处理之后好像就没有问题了。
一、前段时间写了个多线程的代码,当关线程的时候遇到当发送了关线程后,线程不能及时关掉,然后,影响后面的其他的线程的通信。
之前的办法如下:
void MyThread::stop()
{
mutex.lock;
t_stop = true;
mutex.unlock;
mcondition.wakeall;
}
void MyThread::run()
{
forever {
mutex.lock;
if (t_stop) {
t_stop = false;
mutex.unlock;
break;
}
mcondition.wait(&mutex, 15s);//延时15s
mutex.unlock;
dosomething();//会耗时大概10s种;uart和其他模块交互
}
}
更改后解决办法如下:
1、我的硬件平台是arm9
2、子线程里,重新实现了run函数;
t_stop 初始化为false;
大概是这样的:
void MyThread::stop()
{
mutex.lock;
t_stop = true;
mutex.unlock;
mcondition.wakeall;
}
void MyThread::run()
{
forever {
mutex.lock;
if (t_stop) {
t_stop = false;
mutex.unlock;
break;
}
mcondition.wait(&mutex, 15s);//延时15s
mutex.unlock;
mutex.lock;
if (t_stop) {
t_stop = false;
mutex.unlock;
break;
}
dosomething();//会耗时大概10s种;uart和其他模块交互
mutex.unlock;
}
}
3、然后主线程里面先打开,然后过段时间再关闭线程。
关闭:t->stop(); //MyThread t;
就如上所示的关闭线程这样正确吗?
二、这里使用了条件变量。
这样处理之后好像就没有问题了。
相关文章推荐
- 【Qt OpenGL教程】24:扩展、剪裁和TGA图像文件的手动加载
- QTP录制delphi程序,可录制无法运行
- CodeBlocks配置Qt 环境(含:预编译头文件,Qt设计师)
- Qt 5.3 下OpenCV 2.4.11 开发(12)meanShift 简单算法介绍
- Erlang MQTT消息服务器
- Qt 5.3 下OpenCV 2.4.11 开发(11)CamShift 目标跟踪
- QT信号和槽使用自定义的类或结构体
- 解决Qt中QLabel控件加载动态图出现黑色背景的问题
- 图像处理之qt实现
- Qt Mac 在软件 icns图标制作
- 从 Qt 的 delete 说开来
- Qt 的QString类的使用
- VS2010平台下VAssistX和Qt的整合配置及相关便捷操作
- QTableWidget 详细使用
- QT中QTableWidget的使用
- Qt ListView详细用法
- 如何让Qt应用程序只有一个实例
- QT5---QTableWidget简单应用之文件浏览器
- Qt中嵌入Directx11
- Qt中connect的一些使用细节