PDA

View Full Version : qtconcurrent dont close correctly



danics
21st November 2014, 12:14
hi everybody, i want to create a unkillable process so i m using a mq pattern run in different thread like this




void UnkillApp::checkKeepServer()
{
const char* recm = s_recv(socket);
if(recm == NULL)
{
QProcess::startDetached(m_cpath);
}
else if(strcasecmp(recm, "kill") == 0)
{
s_send(socket, "kill");

QProcess p;
p.setProgram(m_cpath);
p.kill();
}

s_send(socket, "keepalive");
}

void UnkillApp::checkKeepClient()
{
int rc = s_send(socket, "areyoualive?");

const char* recm = s_recv(socket);
if(recm == NULL)
{
QProcess::startDetached(m_cpath);
}
else if(strcasecmp(recm, "kill") == 0)
{
s_send(socket, "kill");
QProcess p;
p.setProgram(m_cpath);
p.kill();
}
}

void UnkillApp::run()
{
while (b_run) {
if(b_server)
checkKeepServer();
else
checkKeepClient();

sleep(1);
}
}

void UnkillApp::runInBackground()
{
QtConcurrent::run(this, &UnkillApp::run);
}

void UnkillApp::closethread()
{
b_run = false;
}


and this is the main function



int main(int argc, char *argv[])
{
QApplication a(argc, argv);

UnkillApp s("keepusA", true);
s.runInBackground();

// MainForm w;
MainWindow w;
w.show();

QObject::connect(&w, SIGNAL(closed()), &s, SLOT(closethread()));

return a.exec();
}


but after closing mainwindow in debug mode i can see b_run = false; called but application dont exit and i must kill it why? (and i dont run any other process yet)

anda_skoa
21st November 2014, 18:34
You are accessing b_run from two different threads without any syncrhonisation.

Not saying this is the reason for the observed behavior but wrong nevertheless.

Cheers,
_