学习了几天qt,尝试使用Qt 5.2.0里自带的Qt creator编写一个多线程的小应用:三类线程put、get、move,put线程不断向buffer1放东西,get从buffer2里取东西,move负责将buffer1里的东西放入buffer2,在Mainwindow中设置两个textbrowser,用来实时显示put、get、move的具体动作。现在编译时遇到问题,显示构建错误,忽略了错误执行,主窗口出现了,但两个textbrowser中什么都没显示。
希望高手们能帮帮忙!
MainWindow.cpp:`
#include "mainwindow.h"
#include
#include
#include
QMutex mutex1,mutex2;
QSemaphore free1(12),free2(9); //buffer1与buffer2的空闲区
QSemaphore used1(0),used2(0); //buffer1与buffer2的已用区
QStack stack1,stack2;
bool stop = false;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setGeometry(200,200,1000,800);
this->setMaximumSize(1000,800);
label1 = new QLabel(this);
label1->setGeometry(150,200,100,50);
label1->setText(QString(tr("Buffer1[12]:")));
label2 = new QLabel(this);
label2->setGeometry(600,200,100,50);
label2->setText(QString(tr("Buffer2[9]:")));
pressed = false;
pushbutton = new QPushButton(tr("paused"),this);
pushbutton->setGeometry(470,100,70,50);
textbrowser1 = new QTextBrowser(this);
textbrowser1->setGeometry(150,300,300,450);
textbrowser2 = new QTextBrowser(this);
textbrowser2->setGeometry(600,300,300,450);
for(int i=0;i<PUTNUM;i++)
{
put[i] = new Put();
}
move = new Move();
for(int i=0;i<GETNUM;i++)
{
get[i] = new Get();
}
for(int i=0;i<PUTNUM;i++)
{
put[i]->start();
}
move->start();
for(int i=0;i<GETNUM;i++)
{
get[i]->start();
}
connect(pushbutton,&QPushButton::clicked,this,&MainWindow::pause);
connect(put[0],&Put::putact,this,&MainWindow::dealputact);
connect(put[1],&Put::putact,this,&MainWindow::dealputact);
connect(put[2],&Put::putact,this,&MainWindow::dealputact);
connect(put[2],&Put::putact,this,&MainWindow::dealputact);
connect(move,&Move::moveact,this,&MainWindow::dealmoveact);
connect(get[0],&Get::getact,this,&MainWindow::dealgetact);
connect(get[1],&Get::getact,this,&MainWindow::dealgetact);
connect(get[2],&Get::getact,this,&MainWindow::dealgetact);
connect(get[3],&Get::getact,this,&MainWindow::dealgetact);
connect(get[4],&Get::getact,this,&MainWindow::dealgetact);
}
MainWindow::~MainWindow()
{
delete put[PUTNUM];
delete get[GETNUM];
delete move;
}
void MainWindow::dealputact(char p)
{
QString string1(tr("Put :"));
string1.append(p);
textbrowser1->append(string1);
}
void MainWindow::dealmoveact(char m)
{
QString string1(tr("Move :")),string2(tr("To :"));
string1.append(m);
string2.append(m);
textbrowser1->append(string1);
textbrowser2->append(string2);
}
void MainWindow::dealgetact(char g)
{
QString string(tr("Get :"));
string.append(g);
textbrowser2->append(string);
}
void MainWindow::pause()
{
pressed = !pressed;
if(pressed)
{
pushbutton->setStyleSheet(QString("QPushButton{background-color: qlineargradient(spread:repeat, x1:0, y1:0, x2:0, y2:1, stop:0.00578035 rgba(225, 242, 250, 255), stop:0.536357 rgba(196, 229, 244, 255), stop:0.545299 rgba(150, 209, 241, 255), stop:1 rgba(110, 188, 226, 255));font: 10pt \"\345\276\256\350\275\257\351\233\205\351\273\221\";}"));
}
else
{
pushbutton->setStyleSheet(QString("QPushButton{background-color:none;font: 10pt \"\345\276\256\350\275\257\351\233\205\351\273\221\";}"));
}
if(stop)
{
mutex1.unlock();
mutex2.unlock();
for(int i=0;i<PUTNUM;i++)
{
put[i]->start();
}
for(int i=0;i<GETNUM;i++)
{
get[i]->start();
}
move->start();
}
else
{
for(int i=0;i<PUTNUM;i++)
{
put[i]->terminate();
}
for(int i=0;i<GETNUM;i++)
{
get[i]->terminate();
}
move->terminate();
}
stop=!stop;
}`
Put.cpp
#include "Put.h"
#include
#include
#include
#include
Put::Put()
{
}
void Put::run()
{
extern QMutex mutex1;
extern QSemaphore free1;
extern QSemaphore used1;
extern QStack stack1;
char p;
while(1)
{
if(free1.tryAcquire())
{
if(mutex1.tryLock())
{
free1.acquire();
used1.release();
qsrand(QTime::currentTime().msec());
p=qrand()%26+'a';
emit putact(p);
stack1.push(p);
this->msleep(800);
mutex1.unlock();
}
}
qsrand(QTime::currentTime().msec());
this->msleep(qrand()%100);
}
}
Move.cpp
#include "Move.h"
#include
#include
#include
#include
Move::Move()
{
}
void Move::run()
{
extern QMutex mutex1,mutex2;
extern QSemaphore free1,free2;
extern QSemaphore used1,used2;
extern QStack stack1,stack2;
char m;
while(1)
{
if(used1.tryAcquire()&&free2.tryAcquire())
{
if(mutex1.tryLock()&&mutex2.tryLock())
{
used1.acquire();
free1.release();
free2.acquire();
used2.release();
m = stack1.pop();
emit moveact(m);
stack2.push(m);
this->msleep(800);
mutex1.unlock();
mutex2.unlock();
}
}
qsrand(QTime::currentTime().msec());
this->msleep(qrand()%100);
}
}
Get.cpp
#include "Get.h"
#include
#include
#include
#include
Get::Get()
{
}
void Get::run()
{
extern QMutex mutex2;
extern QSemaphore free2;
extern QSemaphore used2;
extern QStack stack2;
char g;
while(1)
{
if(used2.tryAcquire())
{
if(mutex2.tryLock())
{
used2.acquire();
free2.release();
g = stack2.pop();
emit getact(g);
this->msleep(800);
mutex2.unlock();
}
}
qsrand(QTime::currentTime().msec());
this->msleep(qrand()%100);
}
}
不知道你这个问题是否已经解决, 如果还没有解决的话: