Search code examples
c++windowsmultithreadingxinput

XInputGetState and XInputSetState called from different threads


I have a thread that captures controller input. This is the only thing this thread does - it calls XInputGetState() every x milliseconds and stores the result in a synchronized circular buffer. I have another thread that reacts to this input by vibrating the controller via XInputSetState() when a certain condition has been reached.

In other words, one thread only ever calls XInputGetState(), while the other thread only ever calls XInputSetState(). Theoretically, there can be no point at which either function is being called multiple times simultaneously. Is it still unsafe to do this without synchronization?

Note: These two functions work on an entirely different set of data, but the implementation most likely reads/writes to something that isn't synchronized.


Solution

  • Yes in this case, because the XInput API takes a critical section lock on each external entry-point call.

    BTW, as you are using XInput take a look at this blog post. With just those two APIs, you can use XInput 9.1.0 which is built-in to the Windows Vista and later OS, the headers for it are in the Windows 8.x SDK, and it avoids any need to mess with the legacy DirectSetup to deploy XInput 1.3. Of course, if you are using Windows Store apps, you'd just use XInput 1.4 which is part of the Windows 8.x OS and the headers are in the Windows 8.x SDK. The only situation where you need the use the legacy DirectX SDK is to get audio capture/playback support on Windows 7 or earlier.