Re: best threading solution?
Use a timer instead of a thread.
Re: best threading solution?
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?
Re: best threading solution?
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.
Re: best threading solution?
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)
Re: best threading solution?
Quote:
Originally Posted by
danics
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 :)
Quote:
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.
Quote:
you say a timer exactly run in its interval time and sleep other threads?
The timer has no influence on other threads.
Quote:
( 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.
Re: best threading solution?
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?
Re: best threading solution?
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: [wiki]Keeping the GUI Responsive[/wiki]
Re: best threading solution?
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?
Re: best threading solution?
Quote:
Originally Posted by
danics
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?
Re: best threading solution?
i think with qtimer everything run in a queue so why doesnt decrease of intervals? just that :D
Re: best threading solution?
Quote:
Originally Posted by
danics
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?
Re: best threading solution?
reading serial port faster for getting more data if available :) but my program must run reasonable
Re: best threading solution?
Quote:
Originally Posted by
danics
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".
Re: best threading solution?
:) 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).
Re: best threading solution?
Quote:
Originally Posted by
danics
i cant run a function like my dowork() atomic?
In general no. But why would you want it to be atomic?
Re: best threading solution?
nothing just make sure dont lose data. ofcourse not in this sample, like a process run realy in real time mode
Re: best threading solution?
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
For Qt 5.1
Re: best threading solution?
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).
Re: best threading solution?
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. :) )