NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Using multi threading to access 2 RS232 ports

Hi,
 
I'm a beginner in multi threading, so this question may be a very basic one....
 
I'm Using TS3.5 + CVI 8.01 to communicate withs 2 RS232 ports.  I thought to use mult threading so I can process 2 steps in parallel rather than in series. 
 
This is what I do:
 
1) I defined 2 subsequences, each of them call the same CVI function.  I use the sub sequence parameters to transfer the com number.
 
2) My CVI application includes one dll for this specific project and another one, a general dll to handle RS232 functions.  The RS232 dll uses one global buffer than collects all the traffic on the com. 
 
QUESTIONS:
1) What happens when 2 seperate threads run the same function in the RS232 dll?  (in terms of memory etc...).  Does each one use a different buffer (in terms of memory allocation), or, both of them writes to the same buffer?   Obviously, if they writes to the same buffer, then, my function will not operate properly.
 
2) What happens in TestStand after the 2 steps in new threads have finished their execution?  does the next step run back in the same threads the sequence run before?
 
Thanks
Rafi
0 Kudos
Message 1 of 5
(4,995 Views)
Rafi,

To answer your second question, TestStand creates threads on an as needed basis and reuses any that were previously created when they are no longer being used. This means that if a previously created thread is open, TestStand will use this thread.

Your first question is a bit more complex. First, if you have 2 sequences running in parallel that access the same DLL, it depends on how your DLL is constructed. If your DLL is not multi-threaded, then only one thread can access it at a time. In this case, you want to make sure it is multi-thread safe, meaning it blocks the calls from other threads until the first thread is done. If you have multiple instances of the same DLL, you shouldn't have any problem working in parallel as long as your ports are not on the same device (meaning that the device is being locked by the other thread).

Second, I'm not sure what buffer the threads would access. You did say you were using one global buffer, so I would assume that they both write to the same buffer.

For more information on multi threading and TestStand, you can also visit the links below.
Calling the Same DLL in Multiple Threads in TestStand
Use of Thread.ExternallySuspended within Code Modules to Enable Debugging Multiple Threads
Message 2 of 5
(4,986 Views)

Hi Matt,

Thank you so much for the excellent answere.  I will soon start to experiment with the different issues.

Meantime, I'm wondering how to make my dll multi-thread safe.  Can you point out an example that does it?

Also, in one of the links I read that if a variable is created (e.g. my global buffer) when called by the threads, then, 2 threads can use the same buffer in paralle since it will actually be a separate buffer.  Is that correct?

 

Thanks

Rafi

0 Kudos
Message 3 of 5
(4,957 Views)

Hi Matt,

My program began to run multithreading but i encounter problems...

Since my application calls functions in another dll, I defined a lock object in that dll (created when dll is loaded, and deleted when dll is unloaded.) .  Then, I use cmtGetLock and cmtReleaseLock few places (like writing to log file).

I get a run-time error when I call TS to get a value of a StationGlobal.  I assumed that all calls to CVI, TS are thread safe. Isn't it so?   I put the lock/release around that call but it still fails.  Strangely it alway pass the first time and fail only on the second or third time I run my program.  Any explanation to that?

I read the article "Multithreading in LabWindows/CVI" and I'm wondering if you know of any article that describe how to do multithreading in CVI + TestStand?

for example....the CmtScheduleThreadPoolFunction() is required in CVI.  Is it required when using TestStand?

I read about the different methods to make your dll multithreading safe (lock, variable safe).  It seems much more complex than I initially thought.  Since my dll is already written and not so small, I'm wondering If it is too risky to try and make it multithreading safe.  Can you tell me from your experience is it something worth trying or better to stay away from?

 

Thanks

Rafi

0 Kudos
Message 4 of 5
(4,932 Views)
Rafi,

Glad to hear you were able to make some ground on your application. As for all of your questions, I'll try to answer as many as I can.

First, when you are talking about your global buffer, is it created in TestStand or in the DLL itself? When you use DLLs, global variables or global structures are shared between all threads that call your DLL. On the other hand, if your buffer is declared inside of the DLL it is global for the DLL but not shared and would be a separate buffer for each call.

With your run-time error in TS, it would definitely be helpful to have more information about the error. From what you explained (executing fine on the first call, but failing on future executions), it sounds like the resource is not being released after the first execution.

As far as a specific example for TestStand Multithreading, you'll want to look at the TestStand Development Library and, specifically, Multithreading in TestStand. If you look and browse through the Application Notes and Tutorials section, as well as the Technical Presentations section, you will learn a great deal about multithreading and what options you have in TestStand. For a specific example, you may want to look at This Example. You could also look in the <TestStand>\Examples (where <TestStand> is the directory where TS is installed) at the MultiUUT example for an example of multithreading in TS. These examples may not be exactly what you need, but they should give you a jump start.

As far as making your DLL multithread safe, it is definitely not necessary; however, there are some significant advantages described in this article: Designing Thread-Safe DLLs. It is an MSDN article that focuses on Visual Basic, but it has some helpful information that can apply to C as well.

Hopefully this can help you move further. I have attached a list of links at the end of this post with other helpful links for you as well. Keep us posted with your progress.

Matt Mueller
NI

Links:
General Information on Multithreading for C Programmers

Building Multithreading Applications with LabWindows/CVI

Multithreading in LabWindows/CVI

Easy Multithreading Programming in LabWindows/CVI

Multithreading for Rookies
0 Kudos
Message 5 of 5
(4,920 Views)