服务器与服务器之间的同步客户

时间:2018-02-03 12:34:11

标签: c++ qt synchronization qthread gsoap

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

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

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

我知道这是对这个问题的一个非常糟糕的解释!对不起我的英语。

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

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

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;
 }

MainWindow.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);
}

History.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!

我知道这个输出是正确的。但我怎么能回归QSet ---&gt; ansSet来自MainWindow :: start函数何时发生if(this-&gt; ansSet.size()== st.size())?或者你有一个想法?

伙计们,我很抱歉我的英语:)

1 个答案:

答案 0 :(得分:2)

首先,让我们对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 某处 - 您将 async 无处不在,因为异步和同步不要相处得很好。虽然,您可以等待异步操作完成(例如Qt::BlockingQueuedConnection),但为什么要这样做?如果MainWindow::start阻止MainWindow::on_pushButton_clicked阻止,如果MainWindow::on_pushButton_clicked阻止,MainWindow是您的用户界面窗口,则在按钮上单击所有UI冻结,以防on_pushButton_clicked 1}}是一个服务器功能...你的整个服务器变得反应迟钝。

我建议你做的是下一个:

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.  
}

这样你就不会阻止,只是在他们准备就绪时处理你所有的答案(所有的历史记录都已被记录)。