在qt里写的死锁多线程,但是一直报错error: collect2: error: ld returned 1 exit status;
qt菜鸟感觉自己没哪里写错了,于是死都看不出bug在哪,快要die了
//Pthread.h
#include <QThread>
#include <QDebug>
class Pthread1: public QThread
{
Q_OBJECT
protected:
//线程退出的标识量
volatile bool m_stop;
void run();
signals:
void send_signal(int);//自定义信号
public:
Pthread1();
~Pthread1();
void stop();//线程退出的接口函数,用户使用
};
class Qthread1: public QThread
{
Q_OBJECT
protected:
//线程退出的标识量
volatile bool m_stop;
void run();
signals:
void send_signal(int);//自定义信号
public:
Qthread1();
~Qthread1();
void stop();//线程退出的接口函数,用户使用
};
//Pthread.cpp
#include "Pthread1.h"
#include <QMutex>
#include <unistd.h> //用于sleep
QMutex MutexA;
QMutex MutexB;
Pthread1::Pthread1(){
m_stop = false;
}
Pthread1::~Pthread1(){
}
void Pthread1::stop(){
m_stop = true;
}
void Pthread1::run(){
int step = 0;
while(!m_stop)
{
MutexA.lock();
step = 1; emit send_signal(step);
sleep(1000);
MutexB.lock();
step = 3; emit send_signal(step);
MutexA.unlock();
step = 4; emit send_signal(step);
msleep(1);
MutexB.unlock();
step = 5; emit send_signal(step);
msleep(1);
}
m_stop = 0;
}
Qthread1::Qthread1(){
m_stop = false;
}
Qthread1::~Qthread1(){
}
void Qthread1::stop(){
m_stop = true;
}
void Qthread1::run(){
int step = 0;
while(!m_stop)
{
MutexB.lock();
step = 2; emit send_signal(step);
sleep(1000);
MutexA.lock();
step = 6; emit send_signal(step);
MutexB.unlock();
step = 7; emit send_signal(step);
msleep(1);
MutexA.unlock();
step = 8; emit send_signal(step);
msleep(1);
}
m_stop = 0;
}
//MainWindow.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Pthread1 *p1;
Qthread1 *q1;
public slots:
void send_cmd(int);
void lock();
void unlock();
};
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qmessagebox.h>
#include <semaphore.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h> //用于sleep
using namespace std;
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
p1 = new Pthread1;
q1 = new Qthread1;
connect(ui->pushButton_2_1_b,SIGNAL(clicked()),this,SLOT(lock()));
connect(p1,SIGNAL(send_signal(int )),this,SLOT(send_cmd(int )),Qt::BlockingQueuedConnection);
connect(q1,SIGNAL(send_signal(int )),this,SLOT(send_cmd(int )),Qt::BlockingQueuedConnection);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::send_cmd(int step){
if(step == 1){
//P1 get mutexA
QPropertyAnimation *animation = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry");
animation->setDuration(1000);
animation->setStartValue(QRect(530,140,141,71));
animation->setEndValue(QRect(250,420,141,71));
animation->start();
}
else if(step == 2){
//Q1 get mutexB
QPropertyAnimation *animation1 = new QPropertyAnimation(ui->pushButton_2_1_2,"geometry");
animation1->setDuration(1000);
animation1->setStartValue(QRect(670,210,141,71));
animation1->setEndValue(QRect(110,420,141,71));
animation1->start();
}
else if(step == 3){
//P2 release mutexA
QPropertyAnimation *animation3 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry");
animation3->setDuration(1000);
animation3->setStartValue(QRect(250,420,141,71)); animation3->setEndValue(QRect(390,420,141,71));
animation3->start();
QPropertyAnimation *animation4 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry");
animation4->setDuration(1000);
animation4->setStartValue(QRect(390,420,141,71)); animation4->setEndValue(QRect(390,280,141,71));
animation4->start();
}
else if(step == 4){
//P2 get mutexB
QPropertyAnimation *animation5 = new QPropertyAnimation(ui->pushButton_2_1_2,"geometry");
animation5->setDuration(1000);
animation5->setStartValue(QRect(530,210,141,71)); animation5->setEndValue(QRect(530,420,141,71));
animation5->start();
}
else if(step == 5){
//P2 release mutexB
QPropertyAnimation *animation6 = new QPropertyAnimation(ui->pushButton_2_1_2,"geometry");
animation6->setDuration(1000);
animation6->setStartValue(QRect(530,420,141,71)); animation6->setEndValue(QRect(670,420,141,71));
animation6->start();
QPropertyAnimation *animation7 = new QPropertyAnimation(ui->pushButton_2_1_2,"geometry");
animation7->setDuration(1000);
animation7->setStartValue(QRect(670,420,141,71)); animation7->setEndValue(QRect(670,210,141,71));
animation7->start();
}
else if(step == 6){
//Q2 get mutexA
QPropertyAnimation *animation9 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry");
animation9->setDuration(1000);
animation9->setStartValue(QRect(390,280,141,71)); animation9->setEndValue(QRect(110,280,141,71));
animation9->start();
}
else if(step == 7){
//Q2 release mutexB
QPropertyAnimation *animation10 = new QPropertyAnimation(ui->pushButton_2_1_2,"geometry");
animation10->setDuration(1000);
animation10->setStartValue(QRect(110,420,141,71)); animation10->setEndValue(QRect(110,210,141,71));
animation10->start();
QPropertyAnimation *animation11 = new QPropertyAnimation(ui->pushButton_2_1_2,"geometry");
animation11->setDuration(1000);
animation11->setStartValue(QRect(110,210,141,71)); animation11->setEndValue(QRect(530,210,141,71));
animation11->start();
}
else if(step == 8){
//Q2 release mutexA
QPropertyAnimation *animation12 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry");
animation12->setDuration(1000);
animation12->setStartValue(QRect(110,280,141,71));
animation12->setEndValue(QRect(110,140,141,71));
animation12->start();
QPropertyAnimation *animation13 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry");
animation13->setDuration(1000);
animation13->setStartValue(QRect(110,140,141,71));
animation13->setEndValue(QRect(530,140,141,71));
animation13->start();
}
}
void MainWindow::lock(void) {
q1->start();
p1->start();
q1->stop();
p1->stop();
q1->wait();
p1->wait();
}
顺便想问一句,怎样在qt里一次创建多个线程就像Thread[ ]类似数组那样!!!