PDA

View Full Version : best threading solution?



danics
13th August 2013, 14:08
Hi,
first of all my question may be seemed too open, but i think its not :),

i want to read from a electronic device (from serial port) every 50-100 msec and i think i must added it to a thread i call it main thread, i parse readed data and if i have a good result show some message in my ui,
i have a menu form in my ui too, if user click every of buttons in ui, my main thread must block.
so this is what i want: if main thread run Ui block and if ui run main thread block. i have some messaging mechanism with signals and slots too,

so i must use wait conditions or you have another suggestions?? what you say? :)

my main thread each loop get only 1 second time for run and then can sleep 50 msec or set/release a wait conditon i dont know :D
i have another thread too if another two threads are block, and if its time of my tasks (like 2 times in hour) for downloading and uploading my results, this one must run but dont block their, priority of those is more than this thread and i must send this thread to sleep,
on another hand this thread only have 50-100 msec to run and then must go to sleep until main thread finish one loop work and go to sleep.
but downloading and uploading can run like that????

and my another question about this thread, how this one must be implemment? i mean use same mechanism probably but i must use a event loop for this thread am i right?
this is my download command



class DownloadCommand : public ICommand
{
Q_OBJECT
public:
DownloadCommand(QObject *parent = 0);
DownloadCommand(const QString& url, QObject* parent=0);

void setUrl(const QString& url) { m_url = QUrl(url); }

signals:

public slots:
void onNetworkReply(QNetworkReply* reply);
void downloadCompleted(qint64 d, qint64 size);
QString execute();

private:
QUrl m_url;
QString m_localpath;
QNetworkAccessManager manager;
QNetworkReply* m_reply;
};

DownloadCommand::DownloadCommand(QObject *parent) :
ICommand(parent)
{
}

DownloadCommand::DownloadCommand(const QString &url, QObject *parent) : ICommand(parent)
{
m_url = QUrl::fromUserInput(url);
}

QString DownloadCommand::execute()
{
m_reply = manager.get(QNetworkRequest(m_url));
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadCompleted(qint64,qint64)));
return tr("Download Started...");
}

void DownloadCommand::onNetworkReply(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError)
return;

QByteArray data = reply->readAll();
if(data.length() < 2) return;

QFileInfo fileInfo(m_url.path());
QString fileName = fileInfo.fileName();
if(fileName.isEmpty())
fileName = "unknown.file";

if(!m_localpath.isNull())
fileName = m_localpath + QDir::separator() + fileName;

if(QFile::exists(fileName))
{
int i = 0;
QString newfileName = fileName + ".";

while(QFile::exists(newfileName + QString::number(i)))
++i;

newfileName += QString::number(i);
QFile fm(fileName);
fm.rename(newfileName);
}

QFile file(fileName);
if(file.open(QFile::WriteOnly))
file.write(data);

reply->deleteLater();
m_reply->deleteLater();
}

void DownloadCommand::downloadCompleted(qint64 d, qint64 size)
{
if((d == 0 && size == 0) || (m_reply->error() != QNetworkReply::NoError))
emit processFinished(tr("Cant download file error is %1").arg(m_reply->errorString()));
else
{
if(size != -1)
emit processChanges(100 * d / size);
else emit processChanges(-1);
if(size == d)
emit processFinished(tr("Download is complete"));
}
}

(i copy this code for dont say me give us some code man :)) .)

so i think move it to MyThread and then run myThread.exec() every time that i need to download file. ( i ask this question for correct running signals and slots in thread)

thanks.

wysota
13th August 2013, 14:20
Use a timer instead of a thread.

danics
13th August 2013, 14:28
i use QTimer before but its get so cpu usage for 50 msec call, am i right? this program run on a embedded system

and how can i handle download thread?

wysota
13th August 2013, 14:46
If you use a thread it will also bump up your cpu usage. If you need to do something, it will have to be done by the cpu regardless if you use a thread or not.

danics
13th August 2013, 14:54
i dont mean that, i mean impletation of Qtimer for small intervals may be is not so good i dont remember where i read this so i ask,
and if i use a timer for my main loop how can i run a task thread for downloading and uploading my data?
you say a timer exactly run in its interval time and sleep other threads? ( i think timeout signal go to queue so my other threads can delay running main process)

wysota
13th August 2013, 15:51
i dont mean that, i mean impletation of Qtimer for small intervals may be is not so good i dont remember where i read this so i ask,
Ask the one who wrote it :)


and if i use a timer for my main loop how can i run a task thread for downloading and uploading my data?
You don't need a thread for that.


you say a timer exactly run in its interval time and sleep other threads?
The timer has no influence on other threads.


( i think timeout signal go to queue so my other threads can delay running main process)
Look, it is very simple -- if your program has to do two tasks which take 10 and 20 units of the processor time then regardless if you use threads or not, you will have to spend 30 units of processor time on those tasks. If your machine can do more tasks in parallel than there are tasks waiting to execute (e.g. the processor has multiple cores) then the real time used to complete both tasks (assuming they are independent) will take less than 30 units of time. If your machine has less concurrent threads than there are tasks to execute then the total amount of time spent will be more than those 30 units because the processor will need to switch between tasks. Considering the system has to do more than just handle your two-threaded application, it is very likely it has more tasks to do than threads available. In that case adding more and more threads will slow down the process rather than speeding it up.

danics
14th August 2013, 11:49
ok let me explain more, i want to know witch solution is better, :D
first of all i know i need 30 units, but i have priority for switch task, i need my main thread run with download thread somehow simultaneity but if sleep time of main thread finished the download thread must go to sleep and wait to up until main thread again finish, so in your explanation we cant send a signal to a thread to sleep and call another thread to run, we can do this right? and if you say yes, how much this process better than use timers (in timers mode probably main thread dont run until download thread finish)

we have a loop for main thread and we set a wait condition this is main loop

while(1) {
waitOn(); // check if Ui is in waiting state and if its not set uis and download to wait
doWork();
setOn() // set wait to free
sleep(100);
}

and our problem is download thread, its not run sync so i dont know how implement it? :)
and finally you dont say if i send my sample download command i a thread object and then call exec() function of that thread everything works in event loop of that thread right?

wysota
14th August 2013, 12:12
You don't need any "download thread". It's not that there is a dwarf sitting there and constantly pulling a string that has your incoming data attached to it and which needs constant bugging to get to work. When the data arrives into the socket you will be informed about it, you can read the data (which takes literally miliseconds) and afterwards you can attend to other tasks. When more data arrives, you will again be informed about it, you will read the data and go back to the other task. On the other hand if you do it with threads, your thread will sit around doing nothing 99% of the time and you will have a lot of trouble in synchronizing all your threads.

Your while() loop can be decomposed into a simple timer running with 100ms interval that triggers a slot that calls "doWork()".

Here, read this: Keeping the GUI Responsive

danics
14th August 2013, 13:15
thanks for your answers and useful link. :)
i need a timer or thread for downloading (and of course you say timer, good its easier with timer for me ;)) because its most run 2 times in hour.
so you say i better to cancel downloading if its not run in needed time or ignore it because of run fastly and async (and ofcourse if my file size isn't so big).
thanks alot for your help

Added after 7 minutes:

and sorry last question if i set interval of my main timer to 0, i shouldnt have terrible right? and my cpu usage is less than 100% right?

Added after 28 minutes:

and sorry last question if i set interval of my main timer to 0, i shouldnt have terrible right? and my cpu usage is less than 100% right?

wysota
14th August 2013, 13:26
and sorry last question if i set interval of my main timer to 0, i shouldnt have terrible right? and my cpu usage is less than 100% right?

If you set the interval to 0 then CPU usage will be high. But why do you want to set the interval to 0?

danics
14th August 2013, 13:42
i think with qtimer everything run in a queue so why doesnt decrease of intervals? just that :D

wysota
14th August 2013, 13:57
i think with qtimer everything run in a queue so why doesnt decrease of intervals? just that :D

Why would you want to decrease the interval?

danics
14th August 2013, 14:06
reading serial port faster for getting more data if available :) but my program must run reasonable

wysota
14th August 2013, 15:14
reading serial port faster for getting more data if available :) but my program must run reasonable

The data comes at some set speed. Reading faster will not make you read more data than the sender sends. If the serial speed is 115200bps which is 14400Bps which is 14kBps then if you read every 50ms, you can read at most ~700 bytes at a time all this assuming the sender satitates the port completely (which is rarely the case). Then you have to think how long the data remains valid -- if you read it a second later, will the data still be valid? If so, then why read more often than 1 time per second? The user will not notice more frequent updates to the UI anyway. But maybe you can read once every 2 seconds? What about once per 5 seconds?

"Faster" does not mean "better" and very often it means "worse".

danics
14th August 2013, 16:43
:) no i say my dowork() takes that time not interval, i mean i send 5 or 6 command and wait until they reply so maybe my dowork() get 1 second untill all data that i needed read from board, but my electronic device has a avr with 8mhz clock and so have data every time i need that data (not every time ofcourse but i think every 80msec, i dont know exactly? :D )

Added after 48 minutes:

and one another question :D, i cant run a function like my dowork() atomic? (no big thread or process from system or my program interupt it).

wysota
14th August 2013, 18:51
i cant run a function like my dowork() atomic?
In general no. But why would you want it to be atomic?

danics
15th August 2013, 09:01
nothing just make sure dont lose data. ofcourse not in this sample, like a process run realy in real time mode

kuzulis
15th August 2013, 09:53
2 danics,

In your case there is no need for threads and device polling. It is necessary to use event-based solution instead of polling.

For these purposes you can use QtSerialPort (need do manual build and install if you use Qt 4.8.x, or nothing to do if you use Qt 5.1):

For Qt 4.8.x (http://qt-project.org/wiki/QtSerialPort)
For Qt 5.1 (http://doc-snapshot.qt-project.org/qt5-stable/qtserialport/qtserialport-module.html)

danics
15th August 2013, 11:38
i said not in this sample :), i mean using somthing like PCi Slot and read from pci bus not serialport. its just a question: can we block some function or process to run realtime (without block).

kuzulis
15th August 2013, 12:09
Ahh.. clear.

In any case you can use event-based approach, irrespective of device type.
I assume that in drivers for your custom device it "event-based feature" is implemented?...
(Then everything will be resolved much more simply and more safely. :) )

wysota
18th August 2013, 14:30
nothing just make sure dont lose data. ofcourse not in this sample, like a process run realy in real time mode

You will not lose data.

If you want a process to run with real-time characteristics then you need a real-time operating system and use that system's means to obtain RT scheduling.