PDA

View Full Version : Occurrence Delay in qtconcurrent therds



lamp
26th February 2012, 18:05
Hello
I create three Threads with QtConcurrent :

1- First Thread connect to devise (with Qtimer interval 0.05 seconds and set AvailableValue = 1 if 20 value is reading )

2- Second Thread Control AvailableValue == 1 (in per seconds) and draw graph (qpainter event with Qtimer interval 1 seconds)
(Value saving in qlist )

2- Third Thread (in 5 minut) save value to mysql db (qpainter event with Qtimer interval 300 seconds)

problem

run Second Thread Or Third Thread ,Occurrence delay in First Thread
(if use mouse event in Second Thread, further delay)

How to Resolve this problem?

Code:



Read_timer = new QTimer();
draw_timer = new QTimer();

FirstThread = new QFuture<void>;
watcher3 = new QFutureWatcher<void>;
* FirstThread = QtConcurrent::run(this,&MainWindow::Devs);
watcher3->setFuture(*FirsThread);

SecondThread = new QFuture<void>;
watcher2 = new QFutureWatcher<void>;
*SecondThread = QtConcurrent::run(this,&MainWindow::Graph);
watcher2->setFuture(*SecondThread);

ThirdThread = new QFuture<void>;
watcher1 = new QFutureWatcher<void>;
*ThirdThread = QtConcurrent::run(this,&MainWindow::Save);
watcher1->setFuture(*ThirdThread);

void MainWindow::Devs()
{
connect(Read_timer,SIGNAL(timeout()), this, SLOT(Read_Devs()));
Read_timer->setInterval(50);
Read_timer->start();
}

void MainWindow::Read_Devs()
{
{
adv0->adv1716_read(values_float);
adv1->adv1716_read(values_float);
readconter++;
}

if(readconter==20)
{
tmpValues_float = values_float;
isAvalaibelValus = 1;
readconter=0;
values_float.clear();
}
}

void MainWindow::Graph ()
{
connect(CheckTask_timer,SIGNAL(timeout()), this, SLOT(draw()));
draw_timer->setInterval(1000);
draw_timer->start();
}

void MainWindow::Save ()
{
connect(CheckTask_timer,SIGNAL(timeout()), this, SLOT(Save_mysql()));
draw_timer->setInterval(300000);
draw_timer->start();
}

wysota
26th February 2012, 18:14
Use QThread instances instead of QtConcurrent.

lamp
27th February 2012, 06:53
QtConcurrent Not create real Thead ?!


Use QThread instances instead of QtConcurrent.
QtConcurrent Not create real Thead ?!

wysota
27th February 2012, 08:02
QtConcurrent takes a thread from a thread pool. The size of the pool is equal to the number of cores/cpus in your machine. Thus if you run your three-thread code on a less-than-three-core machine (once you correct all the issues mentioned later), it will never complete. The other issue is that you are using signals and slots and incorrectly assuming the slots will be run in the context of threads running the function that emitted the signal. Moreover you assume your timers will fire in the context of the threads running the start() function -- they will not since you don't have an event loop running for each of those threads and timers need events to be handled. Thus your code will do practically nothing apart starting three timers returning immediately (effectively finishing those threads).

lamp
27th February 2012, 08:20
Thanks
Not enogh time to learn and use QThread , Possible to increase the 'thread pool' size ?
Is there another solution ØŸ

wysota
27th February 2012, 08:52
Increasing thread pool size will not do you any good. That's the least of your problems. Learn to use QThread properly.


QThread *thread = new QThread(this);
thread->start();
MyObject *obj = new MyObject;
obj->moveToThread(thread);
QMetaObject::invokeMethod(obj, "doSomething", Qt::QueuedConnection);

lamp
27th February 2012, 09:26
Increasing thread pool size will not do you any good. That's the least of your problems. Learn to use QThread properly.


QThread *thread = new QThread(this);
thread->start();
MyObject *obj = new MyObject;
obj->moveToThread(thread);
QMetaObject::invokeMethod(obj, "doSomething", Qt::QueuedConnection);

QObject::moveToThread: Widgets cannot be moved to a new thread

wysota
27th February 2012, 09:29
QObject::moveToThread: Widgets cannot be moved to a new thread
That's definitely true. That's why you need to learn to use threads (regardless if you do it with QtConcurrent or with QThread) instead of blindly trying things. At least read the docs for multithreading with Qt.