QT下多线程独占一个变量

fangui 2007-07-16 10:23:21
各位大虾,在我的工程中遇到一个让我很困惑的问题,我的工程需要从一个第三方函数库中读取一个函数,我用优先级不同的线程去读它的值,却发现并没有得到我预想的结果,我把代码贴出来,希望大虾们能够帮我解决这个问题,如下:
/*******第三方提供的头文件*******/
#ifndef APP_H
#define APP_H
typedef struct out{
int x;
int y;
}s; //结构体,x的值可以为1,y的值可以为2
int input(&s) //读入函数,返回1或者0,它从硬件实时读取值,当返回1时,表明结构体s从硬件上得到了数,此时s.x=1 , s.y=2当返回为0时,表明没有硬件的数据传上来;
#define APP_H

/******************定义两个线程thread1和thread2**************************/
,其中s是个结构体,原型为:
#ifndef TEST_H
#define TEST_H
#include <qthread.h>
#include <iostream.h>
#include <qmutex.h>
#include “app.h”

class Thread1:public QThread{
private:
QMtex mutex; //定义互斥锁
out s_1; //定义结构体,用于存放从硬件上读
取的数据
int i,y;
public:
Thread1();
~Thread1();
getdate(); //开启线程,获得硬件上的数据
virtual run(); //执行线程
};

class Thread2:public QThread{
private:
QMtex mutex; //定义互斥锁
out s_2; //定义结构体,用于存放从硬件上读
int j,y; 取的数据
public:
Thread2();
~Thread2();
getdate(); //开启线程,获得硬件上的数据
virtual run(); //执行线程
};
#endif

/*****************************线程函数****************************/
#include "test.h"
Thread1::Thread1(){}
Thread1::~Thread1(){
QThread::wait(); //等待线程结束
}
Thread1::getdate(){
QThread::start(QThread::LowPriority) //开启线程,定义优先级为低级

}
void Thread1::run(){
mutex.lock(); //锁住互斥量
while(1){ //线程一直开着,不能关

i=input(&s_1);//从硬件上读取数据
if(i==1 ){//当硬件有数据且满足条件时执行
cout<<"this is thread1 runing"<<endl;
x=s_1.x;
}
}
mutex.unlock(); //让出互斥量让其他线程运行
}

Thread2::Thread2(){}
Thread2::~Thread2(){
QThread::wait(); //等待线程结束
}
Thread2::getdate(){
QThread::start(QThread::HighPriority) //开启线程,定义优先级为高级

}
void Thread2::run(){
mutex.lock(); //锁住互斥量
while(1){ //这个线程可以关
cout<<"---------thread2 is start"<<endl;
j=input(&s_2);//从硬件上读取数据
if(i==1 ){//当硬件有数据且满足条件时执行
cout<<"this is thread2 runing"<<endl;
y=s_2.y;
break;
}
}
mutex.unlock(); //让出互斥量让其他线程运行
}

/*****************主程序如下********************/
#include "test.h"
int main(int argc, char *argv[])
{
QMutex mutex;
QWaitCondition condition;
Thread1 *thread1;
Thtead2 *thread2;
thread1=new Thread1();
thread1->getdate();

thread2=new Thread2();
thread2->getdate();

return 0;
}
输出结果如下:
---------thread1 is start
this is thread1 runing
this is thread1 runing
---------thread2 is start
this is thread1 runing
this is thread2 runing
this is thread2 runing
.
.
.
感觉优先级比较高的线程Thread2进去过,但是进去后j=input(&s_2)这条语句取不到,而让优先级比较低的Thread1的函数i=input(&s_1)取到了,然后满足Thread1的if(i==1 )条件,输出“this is thread1 runing”后,又转入Thread2的while(1)循环里面,不断输出“this is thread2 runing”
大虾们,我的问题是:为什么线程优先级高的Thread2类里面的不能读到第三方提供的函数j=input(&s_2),而总是被优先级低的Thread1占用呢?我哪里出错了?查看了很多资料,说是如果多线程访问公共变量需要加valotile修饰,但是我的是公共函数,我怎么加阿?有哪位高手可以指点迷津么?
...全文
589 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangui 2007-07-22
  • 打赏
  • 举报
回复
可能没有人会了!
rockefeller8 2007-07-22
  • 打赏
  • 举报
回复
关注。。。
fangui 2007-07-19
  • 打赏
  • 举报
回复
晕死,几天了,都没有人能回答是为什么么?我已经用别的办法获得了我想要的东西,但是我还是想知道,我上面的问题出在哪里了?
fangui 2007-07-17
  • 打赏
  • 举报
回复
就是说不能用优先级来控制线程咯,但是如果我把Thread2::run()函数改成如下所示:
void Thread2::run(){
mutex.lock(); //锁住互斥量
cout<<"---------thread2 is start"<<endl;
while(1){ //这个线程可以关
cout<<"this is thread2 runing---"<<endl;

for(j=0;j<5;j++ ){
cout<<"thread2 run j "<<j<<"times:"<<endl;
}
break;
}
mutex.unlock(); //让出互斥量让其他线程运行
}
输出如下:
---------thread1 is start //开始跑线程Thread1
this is thread1 runing--- //进入Thread1里面run函数里面的while(1)死循环
the x is: 1 //检测到硬件函数input(&s_1)为1,打印x
---------thread2 is start //开始跑线程Thread2
this is thread2 runing--- //进入Thread2里面run函数里面的while(1)死循环
thread2 run j 0 times //j开始计数,j为0
thread2 run j 1 times //j开始计数,j为1
thread2 run j 2 times //j开始计数,j为2
thread2 run j 3 times //j开始计数,j为3
thread2 run j 4 times //j开始计数,j为4
this is thread1 runing--- //继续跑线程Thread1的死循环
this is thread1 runing--- //继续跑线程Thread1的死循环
说明,线程可以用优先级控制的,但就是不知道为什么Thread2的input(&s_2)会泄露进Thread1的input(&s_1)中,让s_1占用去了。
fangui 2007-07-17
  • 打赏
  • 举报
回复
不好意思,void Thread2::run()里面的if(i==1 )应改为if(j==1 )如下:
/*******第三方提供的头文件*******/
#ifndef APP_H
#define APP_H
typedef struct out{
int x;
int y;
}s; //结构体,x的值可以为1,y的值可以为2
int input(&s) //读入函数,返回1或者0,它从硬件实时读取值,当返回1时,表明结构体s从硬件上得到了数,此时s.x=1 , s.y=2当返回为0时,表明没有硬件的数据传上来;
#define APP_H

/******************定义两个线程thread1和thread2**************************/
,其中s是个结构体,原型为:
#ifndef TEST_H
#define TEST_H
#include <qthread.h>
#include <iostream.h>
#include <qmutex.h>
#include “app.h”

class Thread1:public QThread{
private:
QMtex mutex; //定义互斥锁
out s_1; //定义结构体,用于存放从硬件上读
取的数据
int i,y;
public:
Thread1();
~Thread1();
getdate(); //开启线程,获得硬件上的数据
virtual run(); //执行线程
};

class Thread2:public QThread{
private:
QMtex mutex; //定义互斥锁
out s_2; //定义结构体,用于存放从硬件上读
int j,y; 取的数据
public:
Thread2();
~Thread2();
getdate(); //开启线程,获得硬件上的数据
virtual run(); //执行线程
};
#endif

/*****************************线程函数****************************/
#include "test.h"
Thread1::Thread1(){}
Thread1::~Thread1(){
QThread::wait(); //等待线程结束
}
Thread1::getdate(){
QThread::start(QThread::LowPriority) //开启线程,定义优先级为低级

}
void Thread1::run(){
mutex.lock(); //锁住互斥量
cout<<"---------thread1 is start"<<endl;
while(1){ //线程一直开着,不能关
cout<<"this is thread1 runing---"<<endl;
i=input(&s_1);//从硬件上读取数据
if(i==1 ){//当硬件有数据且满足条件时执行
x=s_1.x;
cout<<"the x is:"<<x<<endl;
}
}
mutex.unlock(); //让出互斥量让其他线程运行
}

Thread2::Thread2(){}
Thread2::~Thread2(){
QThread::wait(); //等待线程结束
}
Thread2::getdate(){
QThread::start(QThread::HighPriority) //开启线程,定义优先级为高级

}
void Thread2::run(){
mutex.lock(); //锁住互斥量
cout<<"---------thread2 is start"<<endl;
while(1){ //这个线程可以关
cout<<"this is thread2 runing---"<<endl;
j=input(&s_2);//从硬件上读取数据
if(j==1 ){//当硬件有数据且满足条件时执行
y=s_2.y;
cout<<"the y is:"<<y<<endl;
break;
}
}
mutex.unlock(); //让出互斥量让其他线程运行
}

/*****************主程序如下********************/
#include "test.h"
int main(int argc, char *argv[])
{
QMutex mutex;
QWaitCondition condition;
Thread1 *thread1;
Thtead2 *thread2;
thread1=new Thread1();
thread1->getdate();

thread2=new Thread2();
thread2->getdate();

return 0;
}
我想要的结果应该是:
---------thread1 is start //开始跑线程Thread1
this is thread1 runing--- //进入Thread1里面run函数里面的while(1)死循环
the x is: 1 //检测到硬件函数input(&s_1)为1,打印x
---------thread2 is start //开始跑线程Thread2
this is thread2 runing--- //进入Thread2里面run函数里面的while(1)死循环
the y is: 2 //检测到硬件函数input(&s_2)为1,打印y,退出死循环
this is thread1 runing--- //继续跑线程Thread1的死循环
this is thread1 runing--- //继续跑线程Thread1的死循环

而不是如下程序跑出来的结果:
---------thread1 is start //开始跑线程Thread1
this is thread1 runing //进入Thread1里面run函数里面的while(1)死循环
the x is: 1 //检测到硬件函数input(&s_1)为1,打印x
---------thread2 is start //开始跑线程Thread2
this is thread2 runing--- //进入Thread2里面run函数里面的while(1)死循环
the x is: 1 //跑到Thread1里面的input(&s_1)为1而不是Thread2里
面的input(&s_2),换句话说input(&s_2)被input(&s_1)
占用
this is thread2 runing--- //由于本来属于Thread2的函数input(&s_2)被Thread1的
input(&s_1)取走了,if(j==1 )条件不瞒足,输出
"this is thread2 runing---"
this is thread2 runing--- //继续输出"this is thread2 runing---"
大虾们明白了我的意思么?为什么线程优先级高的Thread2类里面的不能读到第三方提供的函数j=input(&s_2),而总是被优先级低的Thread1占用呢?我哪里出错了?查看了很多资料,说是如果多线程访问公共变量需要加valotile修饰,但是我的是公共函数,我怎么加阿?有哪位高手可以指点迷津么?
dai_weitao 2007-07-16
  • 打赏
  • 举报
回复
thread1=new Thread1();
thread1->getdate();

thread2=new Thread2();
thread2->getdate();

这里的问题, 你的线程1现getdate并且run了, 而此时线程2的对象还没有创建, 高低优先级是没有相对的.

改成
thread1=new Thread1();
thread2=new Thread2();
thread1->getdate();
thread2->getdate();

试试
fangui 2007-07-16
  • 打赏
  • 举报
回复
我的系统是FC6下的KDE下面编程

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧