PDA

View Full Version : QThread help please



munna
25th May 2006, 07:46
Hi,

I have to call a php file with POST request simultaneoulsy for some n times. For this I am using QThread and QHttp. Here is the code.

The thread part



for(int i = 0;i < noOfThreads; i++)
{
MyThread *t = new MyThread();
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
t->run();
}



The Http Part




op->clearPendingRequests();
for(int i=0;i<n;i++) {
QString log = GenerateLog();
QHttpRequestHeader header( "POST", "/somephpfile.php" ) ;
header.setValue( "Host", ipAddress ) ;
header.setContentType( "application/x-www-form-urlencoded" ) ;
http.request(header,QVariant(log).toByteArray());
}



The above code is not working. Can someone please help?

Thanks.
Oh by the way here is my run () function



void MyThread::run()
{
mutex.lock();
Http list(ipAddress, n);
list.SetHost(ipAddress);
list.info();
mutex.unlock();
}


I am new to both threading and http stuff. It would be great if some one can suggest some tutorial or help.

Thanks a lot.

wysota
25th May 2006, 09:45
Where is the mutex defined?

munna
25th May 2006, 10:35
mutex is private member variable of the class MyThread

wysota
25th May 2006, 11:02
You should call t->start() not t->run().

munna
30th May 2006, 12:18
i have tried both, but its not working.

Someone please Help.

Thanks a lot

wysota
30th May 2006, 12:57
I'm not sure but maybe QHttp needs an event loop running? What happens if you call exec() at the end of run()?

munna
30th May 2006, 13:16
Yes, it works but i have another problem now.

I want to post the request in a loop. For that i wrote




for(int i=0;i<noOfSeconds;i++)
{
t= QTime::currentTime();
t.addMSecs(1000);

for(int j=0;j<logsPerSecond;j++)
{
Http list();
list.SetHost(ipAddress);
list.GenerateLog();
exec();

t1 = QTime::currentTime();
if(t1 > t){
break;
}
}
t1 = QTime::currentTime();
while(t1 < t){
t1= QTime::currentTime();
}
}



The above code post the request only once, where as it should have posted more than once.

wysota
30th May 2006, 13:32
Use a timer and in its timeout slot make a new request.

munna
30th May 2006, 13:54
Let me explain it to you in detail.

I have to generate log for n seconds at the rate of m logs per sec. All this should happned in a thread (Let us assume that there are x threads).

Following is my code for it.

x threads are created in a for loop




for(int i = 0; i < x; ++i){
MyThread *t = new MyThread(ipEdit->text(), serialNo, noOfSecsEdit->value(), noOfSecondEdit->value());
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
t->start();
}



The run()




void MyThread::run()
{
QTime t, t1;
for(int i=0;i<noOfSeconds;i++)
{
t= QTime::currentTime();
t.addMSecs(1000);// to know what will be the time after one sec

for(int j=0;j<logsPerSecond;j++)
{
Http list();
list.SetHost(ipAddress);
list.GenerateLog();
exec();
t1 = QTime::currentTime();
if(t1 > t){// if one sec is over then come out of the loop
break;
}
}

t1 = QTime::currentTime();

while(t1 < t){//Wait till one sec is finished.
t1= QTime::currentTime();
}
}
}



Notice that i am not using mutex any more. With the above code log is generated only once.

Can you tell me why is it generated only once whereas it should have got generated more than once ?

Also can you tell me more about even loop and exec() ?

Thanks a lot.

jacek
30th May 2006, 14:38
void MyThread::run()
{
...
for(int j=0;j<logsPerSecond;j++)
{
Http list();
list.SetHost(ipAddress);
...
exec();
...
}
...
}
Is this a real code? IMO it shouldn't even compile.

QThread::exec() is a blocking call and returns after you invoke QThread::exit(). Since you don't invoke it anywhere, only a half of the first iteration of the above loop is executed.


Also can you tell me more about even loop and exec() ?
QThread::exec() starts an event loop in the current thread (just like QCoreApplication::exec() or QDialog::exec()). This way QObjects that live in that thread can receive events.

What is that Http class? Does it use QHttp internally? Are you aware that QHttp works asynchronously?

munna
30th May 2006, 15:15
Is this a real code? IMO it shouldn't even compile.

Yes this is the real code. I comiples and works, but not as expected.


QThread::exec() is a blocking call and returns after you invoke QThread::exit().

I am new to all this. can you please explain this in more detail or some tutorial.


What is that Http class? Does it use QHttp internally?

Yes, the http class inherits QHttp.


Are you aware that QHttp works asynchronously?

Not before you told. But I do not understand this stuff completely. Can you please explain or refer to some tutorial. I'll be greatful to you.

Thanks a lot. Thanks for your time

jacek
30th May 2006, 15:57
Yes this is the real code. I comiples and works, but not as expected.
That's quite interesting. What compiler do you use? Can you compile this program with it?
class Test
{
public:
Test() {};
void foo() {};
};

int main()
{
Test t();
t.foo();
return 0;
}


I am new to all this. can you please explain this in more detail or some tutorial.
Everything you need is in the docs --- just read them carefully. Also check the network/http example.

Your MyThread::run() should look like this:
void MyThread::run()
{
HttpLogger logger;
connect( &logger, SIGNAL( finished() ), this, SLOT( quit() ) );
logger.start();
exec();
}
The rest should be handled by signals & slots of the logger object (or whatever you will call it). HttpLogger::start() should start the timer and after given interval that object should emit finished() signal to stop the thread.

munna
30th May 2006, 17:29
can i know what makes you think that this code should not compile ?

I'll look into everything you have suggested and then probably get back.

Thanks a lot.

jacek
30th May 2006, 18:13
can i know what makes you think that this code should not compile ?

This:
Http list();
it's a forward declaration of a function, but you use "list" like a variable.

munna
31st May 2006, 04:54
oh, I get your point.

Actually the line was




Http list(few parameters);



but I removed them to make the example look simple.

Sorry, It was my mistake.

Thanks for your time and advice.
Thanks a lot.