Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

What is the difference between iblck and CriticalSection

I am programming multi-thread application with Visual C++ on Windows 2000. Each thread calls 488.2 routines.
Should I use iblck for each ibxxx calls? Or is it OK to use WIN32 CriticalSection instead?
0 Kudos
Message 1 of 3
(3,443 Views)
iblck is used for acquiring a lock for a particular board for use by the current process. The intent of iblck is to allow a process to deny other processes from accessing a GPIB board while that process is using it. iblck cannot be used to protect access to a GPIB interface from other threads within the same process.

A CriticalSection is used to protect access to a portion of code. From MSDN documentation on Critical Sections: "When multiple threads have shared access to the same data, the threads can interfere with one another. A critical section object protects a section of code from being accessed by more than one thread. A critical section is limited, however, to only one process or DLL and cannot be shared with other processes."

I belie
ve what you are looking for is a mutex. A mutex is intended to be used to protect access to a shared resource, such as a GPIB board. Again from MSDN documentation: "A mutex object is a synchronization object whose state is set to signaled when it is not owned by any thread and non-signaled when it is owned. Its name comes from its usefulness in coordinating mutually exclusive access to a shared resource. Only one thread at a time can own a mutex object."

I think that the code you're going to write may look something like this:


HANDLE hMutex = CreateMutex(NULL, FALSE, "GPIBMutex");


WaitForSingleObject(hMutex);

ibwrt(ud, "Send a request to instrument");
char responseString[RESPONSE_SIZE];

ibrd(ud, responseString);

ReleaseMutex(hMutex);


Refer to your MSDN documentation for more information on mutexes and other synchronization objects. Please post again if you have more questions.

Craig

Message 2 of 3
(3,443 Views)
Craig,

Thank you for the very helpful response.

ttakai
0 Kudos
Message 3 of 3
(3,443 Views)