gpt4 book ai didi

c++ - 服务器和客户端之间的同步

转载 作者:行者123 更新时间:2023-11-30 03:26:04 25 4
gpt4 key购买 nike

我有一个基于 gsoap 的服务器。Gsoap 也是在 Qt 上编写的。所以我可以使用 Qt 类。

我从客户端向服务器发送请求。

请求如下:服务器提供了一个电话号码和应该发送给他的消息。这个列表可能有 10 000 个或更多,这正是问题所在!我将使用 QThread 向每个号码发送一条消息。当QThread结束它的工作时,应该记录数据库的历史。问题是我们无法理解什么时候所有的历史都记录在数据库中。

我知道这是对问题的非常糟糕的解释!请原谅我的英语。

现在我将尝试向您展示程序的问题。

我有一个 QThread 类,它发送消息。一个历史类,在数据库中写入历史。

void MainWindow::on_pushButton_clicked() //Imagine that this is a server function.
{
QSet<int> st; //List to who sends a message.
st << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
QSet<int> clientAnswer = this->start(st);
qDebug() << "newSet: " << clientAnswer;
//return ClientAnswer list ot client.
}


QSet<int> MainWindow::start(QSet<int> st)
{
for (auto it : st) {
MyThread *thrd = new MyThread(it);
History *hist = new History();
connect(thrd, &MyThread::smsSended, hist, &History::insertHistory);
connect(hist, &History::historyAdded, this, [=](int historyUID){
qDebug() << "history inserted: " << historyUID;
this->ansSet.insert(historyUID);
if (this->ansSet.size() == st.size()) {
qDebug() << "mainwindow finished!";
emit alreadyDone();
}
});
connect(thrd, &MyThread::finished, hist, &History::deleteLater);
connect(thrd, &MyThread::finished, thrd, &MyThread::deleteLater);
thrd->start();
}

return this->ansSet;
}

主窗口.h

private:
Ui::MainWindow *ui;
QSet<int> ansSet; //The list is to send a client.

MyThread.cpp:

class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int tInt, QObject *parent = 0);

signals:
void smsSended(int);

public slots:
void run();

private:
int m_int;
};

MyThread.h

MyThread::MyThread(int tInt, QObject *parent) : QThread(parent)
{
this->m_int = tInt;
}

void MyThread::run()
{
qDebug() << "thread started: " << this->m_int;
emit smsSended(this->m_int * 10);
}

历史.cpp

 History::History(QObject *parent) : QObject(parent)
{

}

void History::insertHistory(int insertInt)
{
qDebug() << "insert History: " << insertInt;
emit historyAdded(insertInt);
}

History.h

class History : public QObject
{
Q_OBJECT
public:
explicit History(QObject *parent = 0);

signals:
void historyAdded(int hInt);

public slots:
void insertHistory(int insertInt);
};

应用程序输出如下:

thread started:  5
thread started: 1
thread started: 3
thread started: 2
thread started: 4
thread started: 7
thread started: 9
thread started: 6
newSet: QSet()
thread started: 8
thread started: 10
insert History: 50
history inserted: 50
insert History: 10
history inserted: 10
insert History: 30
history inserted: 30
insert History: 20
history inserted: 20
insert History: 40
history inserted: 40
insert History: 70
history inserted: 70
insert History: 90
history inserted: 90
insert History: 60
history inserted: 60
insert History: 80
history inserted: 80
insert History: 100
history inserted: 100
mainwindow finished!

我知道这个输出是正确的。但是当发生 if (this->ansSet.size() == st.size()) 时,我如何从 MainWindow::start 函数返回 QSet ---> ansSet?或者您有其他想法?

我很抱歉我的英语不好

最佳答案

首先,让我们对MainWindow::start做一个初步的修正这样我们就可以对此进行推理:

QSet<int> MainWindow::start(QSet<int> numbers)
{
History* history = new History();

for (auto number : numbers)
{
MyThread* sender = new MyThread(number);
connect(sender, &MyThread::smsSent, history, &History::insertHistory);
connect(history, &History::historyAdded, this, [=] (int historyUID) {
qDebug() << "History inserted: " << historyUID;
this->answers.insert(historyUID);
if (this->answers.size() == numbers.size()) {
qDebug() << "MainWindow finished!";
emit alreadyDone();
history->deleteLater();
}
});
connect(sender, &MyThread::finished, sender, &MyThread::deleteLater);
sender->start();
}

return this->answers;
}

好的,完成后问题如下:您在一个地方编写异步(非阻塞)代码(MainWindow::start),但同时尝试在另一个地方同步(阻塞)使用它QSet<int> clientAnswer = this->start(st); .

通常,如果你要async somewhere - 你会async everywhere,因为异步和同步相处得不是很好。虽然,您可以等待异步操作完成(例如 Qt::BlockingQueuedConnection),但为什么要这样做呢?如果MainWindow::start然后 block MainWindow::on_pushButton_clicked block ,如果 MainWindow::on_pushButton_clicked block 和 MainWindow是您的用户界面窗口,然后在按钮上单击所有 UI 卡住,以防 on_pushButton_clicked是服务器功能...您的整个服务器变得无响应。

接下来我建议你做的是:

MainWindow::MainWindow() 
{
connect(this, &MainWindow::alreadyDone, this, &MainWindow::onAllAnswersReady);
}


void MainWindow::start(QSet<int> phoneNumbers)
{
History* history = new History();
for (auto number : phoneNumbers)
{
MyThread* sender = new MyThread(number);
connect(sender, &MyThread::smsSent, history, &History::insertHistory);
connect(history, &History::historyAdded, this, [=] (int historyUID) {
qDebug() << "History inserted: " << historyUID;
this->answers.insert(historyUID);
if (this->answers.size() == phoneNumbers.size()) { // all answers are ready
qDebug() << "MainWindow finished!";
emit alreadyDone();
history->deleteLater();
}
});
connect(sender, &MyThread::finished, sender, &MyThread::deleteLater);
sender->start();
}
}

void MainWindow::on_pushButton_clicked() // Imagine that this is a server function.
{
QSet<int> phoneNumbers; //List to who sends a message.
phoneNumbers << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
this->start(phoneNumbers); // <-- doesn't block
}

void MainWindow::onAllAnswersReady() // <-- a handler function, may belong to any other class
{
qDebug() << "Answers: " << this->answers;
// Do whatever you want with the answers here.
}

这样您就不会阻塞,而只是在准备好所有答案时处理它们(所有历史记录都已记录)。

关于c++ - 服务器和客户端之间的同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com