LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

programming multithreaded applications. To create or not to create

I have an application in which a measurement will take about 4 minutes and I would like to
put it into its own thread. A mintue later, this measurement will happen again.
Is it better to create and then stop the measurement thread each time it is needed, or stay
in the thread and use some mechanism to delay between measurements.
i.e. is it better to create and destroy secondary threads as needed, OR stay within one
secondary thread and just waste time when it is not needed? If the latter, what is a good way
to kill time without pigging the CPU?

thanks
0 Kudos
Message 1 of 6
(3,632 Views)
Best way to kill time is to use Sleep (int n), where n is in ms, rather than Delay (double t), where t is in seconds.
This allows Windows to run other threads efficiently, without hogging the CPU.

JR
Message 2 of 6
(3,626 Views)
I think I didn't get you right.
1) If you start every minute a measurement which takes 4 minutes to complete, you will overcharge your system in all cases.
The max "speed" if your measurement takes 4 minutes, its 1 measurement every 4 minutes.
2) It is usually easier to have waiting threads (with SDK function Sleep NOT CVI function Delay) than creating a new thread on each std timer click. In the later case, you have to be sure than the previous thread is finished before recreating a new one.
Regards,
0 Kudos
Message 3 of 6
(3,620 Views)
I have my thread that gets data, I'm using ProcessSystemEvents in a loop to keep thread open, and just using callbacks in that thread when I need to get data, and that seems to work well.
0 Kudos
Message 4 of 6
(3,613 Views)
hi
sorry that I was not more clear about timing. I just meant that there was some significant amount of time between measurements. The user might walk away for some time. That's why I was thinking about starting and stopping the measurement thread with each measurement.
But it seems like most folks think that it is better to just Sleep().
Do you think that it is intrinsically bad to start and stop threads multiple times?

thanks
bilg
0 Kudos
Message 5 of 6
(3,604 Views)
It is not intrinsically bad to start and stop threads multiple times.
You have to be carefull about:
1) The total number of thread running.
2) The time needed to close all these threads when closing your program.
Regards,
0 Kudos
Message 6 of 6
(3,591 Views)