多线程编程出错 求解

SZ阿辉 2012-04-24 03:39:54
.h文件代码:

#ifndef THREADDIALOG_H
#define THREADDIALOG_H

#include <QMainWindow>
#include<QThread>
#include<QString>
#include <QMessageBox>
#include <QPushButton>
#include<QCloseEvent>
#include<QEvent>


namespace Ui {
class ThreadDialog;
}

class ThreadDialog : public QMainWindow
{
Q_OBJECT

public:
explicit ThreadDialog(QWidget *parent = 0);
~ThreadDialog();

private:
Ui::ThreadDialog *ui;


protected:
void closeEvent(QCloseEvent *event);
private slots:
void startorStopThreadA();
void startorStopThreadB();

};

class MyThead:public QThread
{
Q_OBJECT
public:
virtual void Thread();
void setMessage(const QString &message);
void stop();
protected:
virtual void run();
private:
QString messageStr;
volatile bool stopped;



};




#endif // THREADDIALOG_H


cpp文件代码

#include "threaddialog.h"
#include "ui_threaddialog.h"
#include<QThread>
#include <QMessageBox>
#include <QPushButton>
#include<QCloseEvent>
#include<QEvent>

ThreadDialog::ThreadDialog(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ThreadDialog)

{
ui->setupUi(this);
MyThead threadA;
MyThead threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
threadA.setMessage("A");
threadB.setMessage("B");
threadAButton=new QPushButton(tr("Start A"));
threadBButton=new QPushButton(tr("Start B"));
quitButton=new QPushButton(tr("Quit"));
quitButton->setDefault(true);

connect(threadAButton,SIGNAL(cLicked),this,SLOT(startorStopThreadA()));
connect(threadBButton,SIGNAL(cLicked),this,SLOT(startorStopThreadB()));
}

ThreadDialog::~ThreadDialog()
{
delete ui;
}

void ThreadDialog::startorStopThreadA()
{

if(threadA.isRunning()){
threadA.stop();
threadAButton->setText(tr("Start A"));
}
else{
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}

void ThreadDialog::startorStopThreadB()
{

if(threadB.isRunning()){
threadB.stop();
threadBButton->setText(tr("Start B"));
}
else{
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}

void ThreadDialog::closeEvent(QCloseEvent *event)
{

threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
显示出很多错误,麻烦各位大侠帮忙下
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
SZ阿辉 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
自己顶下
[/Quote]
还有个问题
以下是多线程的代码
#include <QtCore>
#include <stdio.h>
#include <stdlib.h>
const int DataSize = 36; //总共显示在屏幕上的字节
const int BufferSize = 5; //缓冲区的大小
char buffer[BufferSize];

QWaitCondition bufferNotEmpty; //当缓冲区为空时,读取线程会等侍有数据后再执行读操作
QWaitCondition bufferNotFull; //当缓冲区写满时,写入线程会等侍有东西被读走以后再写入
QMutex mutex; //线程间数据的操作
int numUsedBytes = 0; //当前缓冲区的写入字节

//写入线程
class WriteBufferThread : public QThread
{
public:
void run();
};

void WriteBufferThread::run()
{
for (int i = 0; i < DataSize; ++i) {
//在对 numUsedBytes 时执行比较,增加,减少操作时都要进行加锁
mutex.lock();
if (numUsedBytes == BufferSize)
bufferNotFull.wait(&mutex); //缓冲区已满,等侍有数据被读走
mutex.unlock();
//对缓冲区的数据进行写入
buffer[i % BufferSize] = "123456789\n"[i%10];
mutex.lock();
++ numUsedBytes; //每写入一个字节缓冲区使用数加 1
bufferNotEmpty.wakeAll(); //并且告之此时缓冲区肯定不为空
mutex.unlock();
}
}
//读取和打印缓冲区内容线程
class ReadBufferThread: public QThread
{
public:
void run();
};

void ReadBufferThread::run()
{
for (int i = 0; i < DataSize; ++i) {
mutex.lock();
if (numUsedBytes == 0)
bufferNotEmpty.wait(&mutex); //缓冲区已空,等侍有数据写入
mutex.unlock();

fprintf(stderr, "%c", buffer[i%BufferSize]); //打印当前内容到屏幕
mutex.lock();
--numUsedBytes; //每读取一个字节缓冲区使用数减 1
bufferNotFull.wakeAll(); //并且告之此时缓冲区肯定不满
mutex.unlock();
}
fprintf(stderr, "\n");
}

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
WriteBufferThread write;
ReadBufferThread read;
write.start(); //启动写线程
read.start(); //启动读线程
write.wait(); //等侍写线程退出
read.wait(); //等侍读线程退出
return 0;
}
为什么调试的时候会出现 “接收到底层信号”然后调试就无法进行了,而且这个程序好像对读写没有控制
  • 打赏
  • 举报
回复
你这文件通不过编译,
ThreadDialog::ThreadDialog(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ThreadDialog)

{
ui->setupUi(this);
MyThead threadA;//
MyThead threadB;//这两个是局部变量
而你又在类的其它函数中调用,会提示未声明的变量错误
很显明应该将其提升为的类的变量
SZ阿辉 2012-04-24
  • 打赏
  • 举报
回复
自己顶下

21,468

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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