PDA

View Full Version : QThread - how to pause and resume



xrep
7th March 2012, 01:12
Hello guys,

how is possible pause and then resume thread in QT?
My application starts http proxy server in new thread and in some moments I need pause the thread with proxy and get/set data from comunication process between client and server.
I tried this,



proxy::proxy(sqlitt * sq) {

pending = false;
}

void proxy::run(){

smw = new sslMeatWrapper(this);

}

// this method is called from sslMeatWrapper when new request arrives
void proxy::proxyRequest() {
qDebug << "Incoming proxy request";
pending = true;
// in this moment I need to pause thread and get/set data from/to smw object
while (pending)
msleep(1000);

}


In GUI class there is a slot


void sqlitt::next() {
pr->pending = true;
}

but this is not working. The thread is stocked on while statement i suppose. Can you please advice?

Thanks

mentalmushroom
7th March 2012, 07:08
If you modify a variable from a different thread it should be volatile, otherwise compiler may optimize access to it and while loop won't "notice" the change.
Think of the thread safety, accessing the same resource from multiple threads requires protection.
Are you sure you really need a thread in your case? Most things in qt can be done via signal/slot in asynchronous mode.

amleto
7th March 2012, 08:09
Mental, how do you think asynch calls are made without threads :confused:

Op, there isn't some easy function you can call to pause a thread. You should think about what you need to get the data safely - this does not include using non-thread-safe variables and ugly sleeps ;)

Do you really need to do it in 'this' order? Can you make a safer way to communicate between your classes?

mentalmushroom
7th March 2012, 08:17
Socket work asynchronously without threads, so maybe this is what he needs.

high_flyer
7th March 2012, 08:59
how is possible pause and then resume thread in QT?
My application starts http proxy server in new thread and in some moments I need pause the thread with proxy and get/set data from comunication process between client and server.
You should read about general threaded programming concepts.
Specifically about mutexes (QMutex) and wait conditions (QWaitCondition).