我有一个关于从不同线程调用方法的问题。好吧,我正在使用 WinUSB 驱动程序与 USB 设备进行通信。我有单独的线程从设备读取数据。设备命令在主线程中设置。实际上我正在使用 WinUSB_WritePipe 和 WinUSB_ReadPipe 方法来执行此类操作。在读取数据的线程中,我使用具有重叠结构和 WaitForMultipleObject 的异步读取方法。我的设备有一些我需要设置的功能,这是通过主线程中的 GUI 完成的。
我正在观察一些奇怪的行为。我的问题是我是否需要锁定对这些方法的调用(例如,使用互斥锁),所以每次只有一个线程正在访问或调用方法。
旧方式:
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;
新方法:
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;
这是非常简化的代码,其意图仅用于描述我的问题,并不反映我的编程技能。:) 当然,我使用相同的互斥锁,然后在主线程中调用相同的方法。
我希望我尽可能简单地描述我的问题......再说一遍:我需要在不同的线程中锁定对这些方法的调用吗?
感谢您的时间和提前回答。我真的很感激!
兄弟,尼克斯