I have two threads that try to send a command to the same serial instrument possibly at the same time. For example, thread A tries to send "ABC" and thread B tries to send "123". Would the instrument receive the correct strings regardless of the timing?
I'm using Ivi.Visa.IMessageBasedFormattedIO.WriteLine(string) to do that.
If it is NOT thread-safe, I would have to add "lock" to the statement so that only a single thread can execute WriteLine() at a given time.
Answer by Kirk Fertitta · Nov 01, 2016 at 10:34 PM
Officially, the VISA.NET specification does not guarantee thread safety. Additionally, the VISA-C layer that underlies both commercially available VISA.NET implementations also does not guarantee thread safety. VISA-C operations, such as viRead/viWrite may or may not be thread safe, depending upon the VISA vendor implementation.
All that being said, my company did the initial implementation of the VISA.NET buffered I/O code that ships with the VISA Shared Components (and, hence, with both NI and Keysight VISA), and I do know that it was implemented with thread safety in mind. Calling into methods like Printf and Scanf "should" be thread safe, though, again, the VISA.NET specification does not currently require this. Moreover, raw I/O operations were not implemented to be thread-safe, as the intent with those methods was to provide the fastest, most flexible, low-level path to the hardware.
Hope this helps.
Does using IVI drivers require VISA? 1 Answer
Usage of the IVI VISA Dll 7 Answers