NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Running Threads In Parallel

Doug,

 

Thanks for the example.  That makes perfect sense.  I have a few questions for you:

 

1. If for some reason both the main sequence and the polling sequence try to access the lock at the same time which one has priority?

2. In your lock example, why does there need to be a delay at the end of the polling loop sequence (Sleep a little to avoid wasting cpu resources)?  If we didn't put this delay why would the cpu resources be wasted anymore than having the dealy there?

3. Do most people use locks or notifications in TestStand to handle running the main sequence and threads in parallel?

 

Thanks so much!

0 Kudos
Message 11 of 13
(2,279 Views)

Hi testdesign,

 

Although I didn't create this example, I'll try and answer your questions as best I can:

 

1. It's essentially impossible for both threads to access the lock at the same time; neither one has priority over the other.

2. The Wait Step is most likely in there to slow down the While Loop.  If it wasn't there, the loop would execute too fast and take up too many CPU resources.

3. It's difficult to say what people do more; it depends on the situation.  I'm sure you could make either step type work in your case, but if Doug's example works well for you, I'd suggest sticking with it.  I've linked to some help documents to help you differentiate between the step types and determine what you'd rather use.

 

Lock Step: http://zone.ni.com/reference/en-XX/help/370052M-01/tsref/infotopics/sync_step_types_lock/

Notification Step: http://zone.ni.com/reference/en-XX/help/370052K-01/tsref/infotopics/sync_step_types_notification/

Queue: http://zone.ni.com/reference/en-XX/help/370052M-01/tsref/infotopics/sync_step_types_queue/

 

Thanks,

 

Myriam

Message 12 of 13
(2,254 Views)

1. They have the same priority. You should avoid holding the lock any longer than necessary to maximize performance.

2. If you don't do the wait, you are effectively doing what's known in software development as "busy waiting". "busy waiting" consumes CPU resources that could otherwise be used by other threads running in parallel causing those other threads to run slower than necessary. By adding the wait step to the loop, you are allowing the looping thread to be suspended regularly, freeing up the CPU for use by other threads. Typically, suspending the looping thread like this, even for a short amount of time such as 10 to 100 ms, means that that thread will be suspended most of the time, because checking the condition is very fast (much faster than 10 to 100 ms), thus drastically reducing the CPU overhead required for such a loop, and thus allowing other threads running in parallel to execute faster.

3. They are for different purposes. Notifications are typically used to signal a condition has occurred, optionally with some associated data. Locks are for protecting access to a resource or data in order to ensure that only one thread is accessing it at a time.

 

Hope this helps clarify things,

-Doug

Message 13 of 13
(2,233 Views)