I am trying to use the vddk 1.1 in a multithreaded application, all goes fine for one thread but as soon as I kick of mutliple threads I get an AccessViolationException error on the diskLib_Open function.
I did exactly as written on page 24 of the programming guide, one main thread that initializes the library, then I start one or more threads that will "disklib_connect" and opens a seperate thread to get my diskhandle (disklib_open) , once I get the diskhandle I can access the disk. Now this works for the first worker thread but as soon as the a second thread tries to open disk (disklib_open) I get an access violation error ?
Anyone any suggestions on what I am doing wrong ?
Thanks,
Tom
The guidelines for using vixDiskLib in a multi-threaded app are as follows (some of these are unnecessary, we are planning on fixing them)
This is from page 24, please disregard the section at the top.
VixDiskLib_InitEx() or VixDiskLib_Init() should be called only once per process. VMware
recommends that you call them from the main thread.
In the VixDiskLib_InitEx() or VixDiskLib_Init() call, you can leave the logging callbacks as NULL.
This causes VixDiskLib to provide default logging functions, which are not thread safe. If you are using
VDDK in a multithreaded environment, you should provide your own thread safe logging functions.
When you call VixDiskLib_Open() and VixDiskLib_Close(), VDDK initializes and uninitializes a
number of libraries. Some of these libraries fail to work if called from multiple threads. For example, the
following call sequence does not work:
Thread 1: VixDiskLib_Open ...... VixDiskLib_Close
Thread 2: ................................... VixDiskLib_Open ...... VixDiskLib_Close
The workaround is to use one designated thread to do all opens and closes, and to have other worker threads
doing reads and writes.
I think your program may not be following the third guideline.
Thanks
Sudarsan
Tom, can you post some code?
The guidelines for using vixDiskLib in a multi-threaded app are as follows (some of these are unnecessary, we are planning on fixing them)
This is from page 24, please disregard the section at the top.
VixDiskLib_InitEx() or VixDiskLib_Init() should be called only once per process. VMware
recommends that you call them from the main thread.
In the VixDiskLib_InitEx() or VixDiskLib_Init() call, you can leave the logging callbacks as NULL.
This causes VixDiskLib to provide default logging functions, which are not thread safe. If you are using
VDDK in a multithreaded environment, you should provide your own thread safe logging functions.
When you call VixDiskLib_Open() and VixDiskLib_Close(), VDDK initializes and uninitializes a
number of libraries. Some of these libraries fail to work if called from multiple threads. For example, the
following call sequence does not work:
Thread 1: VixDiskLib_Open ...... VixDiskLib_Close
Thread 2: ................................... VixDiskLib_Open ...... VixDiskLib_Close
The workaround is to use one designated thread to do all opens and closes, and to have other worker threads
doing reads and writes.
I think your program may not be following the third guideline.
Thanks
Sudarsan
Public Delegate Function getDiskHandle(byval connection as intptr, Byval filename as string) as intptr
Class Main
Shared m_getdiskHandle As getdiskHandle
Public Shared Sub main()
m_getdiskHandle = New getdiskHandle(AddressOf getMyDiskHandle)
DiskLib_Init(1, 1, Nothing, Nothing, Nothing, Nothing)
Dim nT As Thread = New Thread(AddressOf doDiskTask)
nT.IsBackground = True
nT.Name = "Thread 1"
nT.Start()
' So far so good !
Dim nT1 As Thread = New Thread(AddressOf doDiskTask)
nT1.IsBackground = True
nT1.Name = "Thread 2"
nT1.Start()
' Second thread tries to open the disk handle and I get an accessviolation error !?
End Sub
Public Shared Function getMyDiskHandle(ByVal connection As IntPtr, ByVal fileName As String) As Intptr vixDisk.Open(connection, fileName, Nothing, diskPtr)
return diskPtr
End Function
Public Shared Function doDiskTask()
Dim cnxParamsUID As VixDiskLibConnectParams = New VixDiskLibConnectParams
cnxParamsUID.serverName = Me.serverName
cnxParamsUID.credType = VixDiskLibCredType.VIXDISKLIB_CRED_UID
cnxParamsUID.creds.uid.userName = m_userName
cnxParamsUID.creds.uid.password = m_password
cnxParamsUID.vmxSpec = "moref=" + vmMor.Value
cnxParamsUID.port = Nothing
vixDiskLib.ConnectEx(cnxParamsUID, True, Nothing, Nothing, connPtr)
diskPtr = main.getMyDiskHandle(connPtr, diskBacking.fileName)
End Function
End Class
Yes you were right, I didnt follow the 3rd guideline. its working now.
Thanks