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 [email protected], 
        [email protected] 
          2011-03-21 
_______________________________________________
webkit-qt mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-qt

Reply via email to