[webkit-qt] about qt's multi thread

周鹏 zhoupeng19 at 126.com
Mon Mar 21 00:18:29 PDT 2011


Hi,all! 

    I want to implementation a  Javascript parse server,which used threadpool.So, when the program started, the main thread created some child thread,and main thread accept input data(the html code),distribute the input data to child thread, child thread execute the Javascript in the input data. when the input data has been processed,the child thread return the output data(the html code after Javascript has been execute) and wait for next job.

I read some article,and write some code,but i can't understand the multithread part of qt,so, can anybody help me to finish my program,or give me some pseudo code? (I'm a newbie, and i have not enough time to study qt's multithread)

Thanks!


class WebCapture : public QObject
{
    Q_OBJECT
public:
    WebCapture();
    void parse(); 
 QString getOutput();
signals:
    void finished();
private slots:
    void saveResult(bool ok); 
private:
    QWebPage m_page;
 QString output;
};
WebCapture::WebCapture(): QObject()
{
    connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(saveResult(bool))); 
} 
void WebCapture::parse(QString source,QString base_url)
{ 
     output = "";
     QUrl url(base_url);
     m_page.mainFrame()->setHtml(source, base_url);
     return ;
}
void WebCapture::saveResult(bool ok)
{
    if (!ok) 
    {
        emit finished(); 
        return; 
    }
    output = m_page.mainFrame()->toHtml.toStdString();
    emit finished();
}
QString WebCapture::getOutput()
{
     return output;
}

class Thread : public QThread
{
     Q_OBJECT
public:
     void run();
     wait4data();//wait for input,blocked
signals:
private:
     WebCapture wc;//which is used to process input data
};
Thread::wait4data()
{
 //TODO
}
 
void Thread::run()
{
    QApplication a(argc, argv);
    QObject::connect(&wc, SIGNAL(finished()), QApplication::instance(), SLOT(quit()));
    while(true)
    {
        //TODO
       wait4data();
       wc.parse(inputdata);
       a.exec();
       //send output to main thread
    }
}
#include "JSServer.moc"
int main(int argc, char* argv[])
{
    int threads = 10;
    Thread* ptr = new Thread[threads];
    for(int i = 0; i < threads; ++i)
    {
        ptr[i].start();
    }
    while(true)
    {
        wait4input();//wait input data
       distribute();//distribute input data to a idle child thread,and i think the output should returned asychronous
    }

    for(int i = 0; i < 4; ++i)
    {
        ptr[i].wait();
    }
    return 0;
}


        Zhou Pengwebkit-qt at lists.webkit.org, 
        zhoupeng19 at 126.com 
          2011-03-21 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20110321/b4f0cc83/attachment.html>


More information about the webkit-qt mailing list