Does the method and functions call

I have a question about method calls from different threads. Well, I use the WinUSB driver to communicate with a USB device. I have a separate stream for reading data from the device. Commands for the device are specified in the main thread. In fact, I use the WinUSB_WritePipe and WinUSB_ReadPipe methods to perform such operations. In the stream where the data is read, I use an asynchronous read method with an overlapped structure and WaitForMultipleObject. My device has some functions that I need to install, and this is done through a graphical interface in the main thread.

I am observing some strange behavior. My question is that I need to block calls (e.g. using a mutex) for these methods, so there is only one thread at the time of access or a call method.

OLD WAY:

type TMyThread = TThread protected procedure Execute; override; end; procedure TMyThread.Execute; begin while not Terminated do begin WinUsb_ReadPipe(Pipe, Amount, Overlapped) ErrNo := GetLastError; if ErrNo = ERROR_IO_PENDING then begin wRes = WaitForMultipleObjects(2, @HndEvt, false); if wRes = WAIT_OBJECT_0 then begin ResetEvent(Overlapped.hEvent); WinUSB_GetOVerlappedResult DoSomethingWithData; // Do something end; end; end; end; MainThread: begin // Set device sample rate WinUSB_WritePipe (Pipe, Amount, Data, ...) end; 

NEW WAY:

 type TMyThread = TThread protected procedure Execute; override; public procedure Lock; procedure Unlock; constructor Create(ASuspended: boolean); override; destructor Destroy; override; end; constructor TMyThread.Create(ASuspended: boolean); begin hMtx := CreateMutex(nil, false, nil); end; destructor TMyThread.Destroy(ASuspended: boolean); begin CloseHandle(hMtx); end; procedure TMyThread.Lock; begin WaitForSingleObject(hMtx, 10000); end; procedure TMyThread.Unlock; begin ReleaseMutex(hMtx); end; procedure TMyThread.Execute; begin while not Terminated do begin Lock; WinUsb_ReadPipe(Pipe, Amount, Overlapped) Unlock; ErrNo := GetLastError; if ErrNo = ERROR_IO_PENDING then begin wRes = WaitForMultipleObjects(2, @HndEvt, false); if wRes = WAIT_OBJECT_0 then begin ResetEvent(Overlapped.hEvent); Lock; WinUSB_GetOVerlappedResult Unlock; DoSomethingWithData; // Do something end; end; end; end; MainThread: begin // Set device sample rate Lock; // same mutex as in TMYThread WinUSB_WritePipe (Pipe, Amount, Data, ...) Unlock; // same mutex as in TMYThread end; 

This is a very simplified code, which is intended only to describe my problem and does not reflect my programming skills. :) Of course, with the same mutex I block, then I call the same method in the main thread.

I hope I described my problem as simple as possible ... so again: do I need to block calls to these methods in different threads?

Thanks for your time and answers in advance. I really appreciate it!

Br, Nix

+6
source share
1 answer

if you download and read winUSB white paper , you will find that there is one big drawback to winUSB: it does not support multiple simultaneous applications for the same USB device. IMHO means you cannot have concurrent calls to read.

+3
source

Source: https://habr.com/ru/post/915310/


All Articles