LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Timers In Multitreading

Hi,

Am working ona  application which receives data from four COM ports simentaniously,i have implemnted four threads to receive each of the channel.

i have a timing requirement for each channel, means i should read each channel at a diffrent interval 10,15,10,25 msec respectively.

what would be the best way to implement the timer for each of the tread independently to make it continuiosly receive in the above mentioned time frame until user stops the application.

 

Thanks in Advance

0 Kudos
Message 1 of 14
(5,534 Views)

Hello, IVI!

 

One way you could do this application, is to use CVI timers. You could program your timers individually to be triggered at specific intervals:

http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/uiref/cviprogramming_with_timer_controls/

You can also check some timer sample applications that ship with CVI, to get you started.

 

Best regards,

- Johannes

0 Kudos
Message 2 of 14
(5,516 Views)

Hi,

Thanks For you reply,

i came through the function "NewAsyncTimer (1.0, -1, 1, MyTimerCallback, 0)"

so this function seems to start the function call (MyTimerCallback) in the secondary thread once the clock ticks.

so if i create four of above function and assign for four diffrent receive corresponds for each serial channel.

each of the receive should work in diffrent thread aftercorresponding clock ticks?

 

please correct me if my understanding is wrong.

 

 

Thanks In advance

0 Kudos
Message 3 of 14
(5,512 Views)

Async timers run in a separate thread, but all timers share the same thread!

This means that interference may happen between timers, in the worst case (depending on how long takes callback to execute) some timer may not run at all: see this thread for reference.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 14
(5,500 Views)

Yes IVI!

 

What you suggested and what 

 

 

0 Kudos
Message 5 of 14
(5,491 Views)

Thanks,

so even if i create four Ansyc timer callback all four callback will be running in the same thread.

but i have a requirement for two of the callback  runs at same time interval (50ms).i need to aquire data from two channels at a rate of 50ms

so i suppose async timer is not a good solution for me?

 

Thanks in advance

0 Kudos
Message 6 of 14
(5,480 Views)

Hello IVI,

 

I think you might have misunderstood what RobertoBozzolo proposed.

An Asynchronous Timer will run in its own thread, independent from your thread pool, but normal Timers run all in the main CVI thread, as D_Biel explains in the post Roberto mentioned.

 

Here are some more resources, describing asynchronous timers:

Best regards!

- Johannes

0 Kudos
Message 7 of 14
(5,475 Views)

Hi,

if i create four async timer as below in the aplication,to receive four of my serial channel

 

ID_async_timer1 = NewAsyncTimer (10.0, -1, 1, MyTimerCallback1, 0);

ID_async_timer 2= NewAsyncTimer (10.0, -1, 1, MyTimerCallback2, 0);

ID_async_timer3 = NewAsyncTimer (10.0, -1, 1, MyTimerCallback3, 0);

ID_async_timer4 = NewAsyncTimer (10.0, -1, 1, MyTimerCallback4, 0);

 

will all the four timers spawn 4 diffrent thread ? 

because irrespective of other channel i have to recive in a channel for every 10 seconds?

 

Thanks in advance

0 Kudos
Message 8 of 14
(5,473 Views)

As far as I can understand, all callbacks will be executed in the same thread. This implies that under some circumstances some timer callback can never be executed: callback lenght is crucial here (see D_Biel post and code here in the thread I posted earlier: experiment with the code as is and by running timer2 before timer1).

 

I can think of a more complex architecture that may be able to sustain your requirements: it won't be trivial to trim it at best but on modern multicore PCs I suppose is the best you can have:

  • At program start launch four different threads, one for each COM port to handle; threads mus be sitting in a loop that processes events, possibly with some small delay in it. Save thread IDs for use in the timer callbacks
  • Launc your 4 async timers with required intervals
  • Timer callbacks will simply PostDeferredCallToThread the required function, using the appropriate thread ID saved before. Make sure that launched callbacks do not last more that the corresponding timer interval
  • Provide a mean to stop all threads at program end

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 14
(5,457 Views)

IVI, I should slightly correct my reply.

So Asynchronous Timers will all be scheduled to run in a common thread. This page states that Windows Asynchronous Timers all share one thread. It's only on RT, that each timer will own its dedicated thread.

 

So the best solution on Windows, would be to use dedicated threads.

In order to schedule these threads, you can either use the PostDeferredCallToThread method (or even events) described by 

 

0 Kudos
Message 10 of 14
(5,446 Views)