LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Timers In Multitreading

Hi,

I have created four  threads by

 

 CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, receive_com1, NULL, &com1Id);
  CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, receive_com2, NULL, &com2Id);  
  CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, receive_com3, NULL, &com3Id);  
  CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, receive_com4, NULL, &com4Id);

 

and i use Delay() Function in each of the tread to do the hold the receive for a desired time

 

do you see any problem with this implementation?

 

 

Thanks in advance

0 Kudos
Message 11 of 14
(2,370 Views)

Is it critical for each individual thread to perform reads at exact time intervals? Or is sufficient, if each threads perform the read operation just periodically (i.e. the exact read time doesn't matter).

If the latter is true, then you should be fine with your Delay-implementation. The point that I'm trying to make is that the read operation might cause some intervals to be greather, if the read takes longer than expected. This way, your read intervals might increase and read operation time might slide. This could pottentially cause the read operations between the 4 threads to overlap.

 

However, if the exact read time is not a requirement, you should be fine with your implementation.

 

As a side note, you might want to use Sleep() instead of the Delay() function, because Sleep() will relinquish the CPU to other threads to execute, while the Delay() function will cause a busy-wait:

http://digital.ni.com/public.nsf/allkb/249E22A8822445D386256D9D0056D00E

 

Best regards!

- Johannes

0 Kudos
Message 12 of 14
(2,362 Views)

Thanks for your response,

yes you are correct.

i have a requqirement for each read operation to be happended in paticular time interval.

what would be the better way to do that ,please advice.

 

Thanks in Advance

 

0 Kudos
Message 13 of 14
(2,352 Views)

One good option would be to implement 


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

This approach will ensure that exact interval times are respected and if an iteration takes longer, the "event" posted in the queue, will ensure that you you'll recover from it and still perform the read, even though it will be scheduled slightly later.

 

However, this complicated approach is only suited if the thread simpler thread-approach doesn't work, beause a read operation is highly likely to take very long. If you are very shure, that the read operation won't overlap other iterations, Async Timers are also suited.

 

- Johannes

0 Kudos
Message 14 of 14
(2,346 Views)