#include "socket.h"
#include "ui_socket.h"
#include<QMessageBox>
#include<QFileDialog>
Socket::Socket(QWidget *parent) :
QWidget(parent),
ui(new Ui::Socket)
{
ui->setupUi(this);
}
Socket::~Socket()
{
delete ui;
}
//使用单例模式
Socket *Socket::getInstance()
{
// 最基础的用法,简单粗暴有效,适用于单线程场景
// static Socket instance; //局部静态变量,若定义为指针,则需要手动释放内容
// return &instance;
//双重校验检索方法(应用在多线程场景中)
static QMutex mutex;
static Socket *instance;//关闭程序时自动析构(delete ui)
if(!instance) {
QMutexLocker locker(&mutex);//加锁,锁内只有一个线程执行
if(!instance) {//先执行的线程会进入内部new对象,后一个线程判断instance就不是NULL了
instance = new Socket;
}
}
return instance;
}