LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

do multiple async timers execute their callbacks in the same thread?

When using multiple async timers do they execute their callbacks in the same thread or does each async timer callback get its own thread?

thanks,
Lorrie
0 Kudos
Message 1 of 10
(5,229 Views)
Hi Lorrie,

When you create multiple async timers, one single common thread is created for all of their callbacks. The reason for this is that the main purpose of creating a separate thread for the callback is to isolate the callback from the user interface thread, not necessarily another timer callback. Most use cases involve just one timer callback, which only needs to be separated from the user interface. If you were to create an application that required multiple threads to run at the same time based off of some timing mechanism, you could try a single timer that has a callback to spawn a new thread and perform whatever operation you need to. Hope this helps!

Jeremy L.
National Instruments
Jeremy L.
National Instruments
0 Kudos
Message 2 of 10
(5,228 Views)
Hi,
I want to develop an application which reads all 5 ports of a RS-485 card. I want to do this using Async timers independently for all the 5 channels.Can i include 5 Async timers with different callbacks , one for each channel so that there would be no dependency between them and i am very particular about the timing issues( time stamping of the received message).Please suggest a suitable method for the implementation of this application.

Regards,
Siddu
0 Kudos
Message 3 of 10
(5,228 Views)
Is there a reason why you want to use async timers to do this? On the face of it I would just create a new thread pool and run the same function in 5 separate threads. Each thread would check for messages on its associated port and time stamp them when received. Just make sure you dump each threads remaining time quantum if there is no data to read on the port (use Sleep(0) or similar from the Windows SDK). This will make the time stamps more accurate.

From my experience, async timers run in the first secondary thread, i.e. the not the main thread that should run the GUI, regardless of what else might be running in that thread. This could cause problems with anything else that wants to run in that thread as asynchronous procedure calls (as queu
ed by the async timer) will take precendence, and you would probably want to set a very short time interval on your timers.

Reply if there is a reason why this approach won't work for you.

Regards

Jamie Fraser
0 Kudos
Message 4 of 10
(5,228 Views)
Hi,
I could not understand your solution. I am not too particular about Async timers.
My application is as follows-
1.Configure 5 ports of RS-485 card.
2.Keep reading the data at the ports. Collecting data at each port should be independent of the other.

I tried using the normal CVI timers one for each channel and under the callback i tried to read the data at the port. But it was taking around 90% of CPU usage.

Then i tried with the Async timer. That works fine in a single thread (reading the data at ports one by one under the callback).
Is there any other way i could make the cyclic reading of ports independent of eachother under different threads as i am very particular about the timing issues.

Regards,
Siddu
0 Kudos
Message 5 of 10
(5,228 Views)
Hi,

Start by writing a function that continuously checks for data on one RS485 port. When data is available, read it from the port. When you are happy with this function, you need to make it suitable for multi-threading. You will need to read some of the Application Notes on multi-threading in CVI available on this site. They include all you need to know.

Basically you need to make your function check for data, if none is available tell Windows (via Sleep(0)) that this thread can be paused and scheduled to run again a bit later on. If data is available, read it and store it. Then, pass it to another thread function where the data can be processed as you wish.

You then simply spawn five instances of this same function in
five different threads, telling each instance of the function which port you want it to read via a passed parameter. Windows will then ensure that each instance will get a slice of processor time, pretty much regardless of what else the machine is doing.

Have a look around on this site and in the CVI Utility->Multithreading library and see if you can work out how to do it.

Hope this helps a bit.

Jamie
0 Kudos
Message 6 of 10
(5,229 Views)
Jamie,

That helped me a lot. Thank you.

Regards,
Siddu
0 Kudos
Message 7 of 10
(5,228 Views)

Jaime,

My current application is very similiar... you have saved me from wasting a lot of time... One question just to clarify.. Once I have written the threaded function I am happy with, I can specify that callback to as many seperate thread instances I desire and they will each have their own instance of that function? I ask because I have many static variables in the function, but I definitely need a different instance of those statics for each thread... Thanks.

0 Kudos
Message 8 of 10
(4,867 Views)
The function's static variables will not be replicated for each thread.  You will need thread-local variables for your function.  I recommend you take a look at the example program in samples\utility\Threading\ThreadLocalVar.  It demonstrates how to use the Thread Local Variable functions in the Utility library, which will help you acheive your goal.

Hope this helps.

Mert A.
National Instruments
0 Kudos
Message 9 of 10
(4,864 Views)
Thanks for the help. The example is very straightforward and informative....
 
Stephen.
0 Kudos
Message 10 of 10
(4,858 Views)