LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

multithreading and timers

Hi everybody,

I have a question concerning multithreading and timers in CVI.

I would like to read the inputs and outputs of a certain I/O device that is connected to my computer over ethernet. I use TCP sockets for that purpose.

To do that, I thought about using two timers running in two seperate threads. One for the inputs, the other one for the outputs of course.

So I managed to create the two threads with the "CmtScheduleThreadPoolFunction" and created a callback function for each of them.

Further on I created two timers, which are started in the two thread callback functions by the "SetCtrlAttribute" function.

Now the question is:

Do the timers really run in the two threads just because I started them in the callback func
tions?

Cause when I release the two threads both timers are still active and try to read their information from the tcp socket. I thought the timers would stop when I destroy the threads!?

And a second question:

Does it make sense to let the timers run in seperate threads anyway. Is there a performance advantage compared with just letting them run in the main thread?

I'm a newbie to multithreading so every help would be very very much appreciated.

And please feel free to ask if you don't understand my wish-wash. 🙂

Regards.

Marc.
0 Kudos
Message 1 of 6
(4,507 Views)
You talking about the async timer. I think all of those timers run in 1
separate thread, whether you create them in your main threads or other
threads. Underneath, they are using microsoft's multi-media timer.

If you already have created 2 threads for your data I/O, why not use
them themselves to do your I/O. You use async timers, when you don't
want to create separate threads to simply things.

vishi

Marc Neugebauer wrote:
> Hi everybody,
>
> I have a question concerning multithreading and timers in CVI.
>
> I would like to read the inputs and outputs of a certain I/O device
> that is connected to my computer over ethernet. I use TCP sockets for
> that purpose.
>
> To do that, I thought about using two timers running in two seperate
> threads. One for th
e inputs, the other one for the outputs of course.
>
> So I managed to create the two threads with the
> "CmtScheduleThreadPoolFunction" and created a callback function for
> each of them.
>
> Further on I created two timers, which are started in the two thread
> callback functions by the "SetCtrlAttribute" function.
>
> Now the question is:
>
> Do the timers really run in the two threads just because I started
> them in the callback functions?
>
> Cause when I release the two threads both timers are still active and
> try to read their information from the tcp socket. I thought the
> timers would stop when I destroy the threads!?
>
> And a second question:
>
> Does it make sense to let the timers run in seperate threads anyway.
> Is there a performance advantage compared with just letting them run
> in the main thread?
>
> I'm a newbie to multithreading so every help would be very very much
> appreciated.
>
> And please feel free to ask if you don't understand my wish-wash. :
)
>
> Regards.
>
> Marc.
0 Kudos
Message 2 of 6
(4,507 Views)
Marc,

There are two kinds of timers in CVI: UI timer controls, and asynchronous timers. I'm assuming that the ones you're using now are timer controls, since you refer to the SetCtrlAttribute function. In that case, you should know that the timer events are sent in the thread that *owns* the panel in which the timer controls are created. This is the thread in which you called LoadPanel or NewPanel. The bottom line is that timer controls are not intended to be used in different threads -- to do so, you'd have to place them in different panels, and then process events (via ProcessSystemEvents, GetUserEvent or RunUserInterface) in each thread where you'd want to receive events.

The other type of timer is the asynchronous timer object that you can use via the toolsl
ib\toolbox\asynctmr.fp library. These are in fact intended to be used in multithreaded applications. Each timer that you create sends its events in its own thread. You don't even have to create the threads yourself with the CmtScheduleThreadPoolFunction. The timers do that for you. (Hint: when you're breakpointed, you can see what thread you're in by selecting Run>>Threads).

The disadvantage of using multiple threads is that you'll have to be very careful in protecting your global data, and you have to deal with possible race conditions, or you might even need to define critical sections in your program. If you're new to multithreaded programming, I recommend reading the "Multithreading in LabWindows/CVI" white paper that is included in the CVI installation. You can find it in the documentation shortcuts in the CVI section of the Start Menu.
The advantage of having multithreaded timers is that they are much more reliable than the timer controls, and you won't have to worry about ru
nning code in your main thread that might "block" your timers.

Hope this helps

Luis
NI
Message 3 of 6
(4,508 Views)
Hi LuisG,

THX for your explanation! I'm new too at multithreaded programming and async timer...

I have a question for you: I looked at this example ftp://ftp.ni.com/pub/devzone/epd/async_timer.zip and tried to replace the UI timer by another async timer.

Here is my question: how do you stop only one timer (in my try, I have 2 async timers) since the "SuspendAsyncTimerCallbacks" function included in the asynctmr.fp stop all the timers ?

I ask this because I have to develop a software which simulates 2 equipments. For the first I need to have a cycle of 6sec and a event which occurs in each cycle after 1.5sec. For the second one, the cycle is 1.6sec and the event occurs in each cycle after 0.5sec.

I'd like to stop one or more timer to see the impact of these on the others.

By the way, how could I set 2 timers at the same rate but "desynchronised" in order to simulate the cycle and the event in it?
0 Kudos
Message 4 of 6
(4,163 Views)
You can use SetAsyncTimerAttribute function with ASYNC_ATTR_ENABLED attribute set to 0 (disable) or 1 (enable).
Hope this helps.
S. Eren BALCI
IMESTEK
Message 5 of 6
(4,160 Views)
THX elbaci and sorry because it was obvious...
0 Kudos
Message 6 of 6
(4,155 Views)