Win32CaptureSample icon indicating copy to clipboard operation
Win32CaptureSample copied to clipboard

some questions about threading model

Open mike07026 opened this issue 2 years ago • 1 comments

First of all, thank you for providing the sample code and discussing the details with us. I learned a lot from them. After carefully reading your sample code and API documentation, I still have some questions about WGC. It will be appreciated if you could reply. Thank you in advance.

  1. Is it safe to use MTA thread to access Direct3D11CaptureFramePool (create, initial, recreate, close, TryGetNextFrame , relaseFrame ) ?

  2. Is it safe to call Direct3D11CaptureFramePool::TryGetNextFrame on one thread, get and hold the Direct3D11CaptureFrame object for a while, and finally release the Direct3D11CaptureFrame object on another thread ? I notice that release the Direct3D11CaptureFrame object trigger some call on Direct3D11CaptureFramePool , so is it necessary to use a std::mutex to avoid concurrent operation?

  3. There is only one IDirect3DDevice in my code, and all multithreading access to the device's ImmediateContext is protected by a std::mutex. If I pass the same IDirect3DDevice to Direct3D11CaptureFramePool::Create, should I lock on the std::mutex before calling some WGC's API to protect the device's ImmediateContext ?

mike07026 avatar Jan 06 '24 18:01 mike07026

I'm glad you've found the repo useful!

As for your questions:

  1. It is OK to use an MTA thread.
  2. Closing the Direct3D11CaptureFrame on another thread should also be OK.
  3. The issue with using your own lock/mutex is that other components using your device context can't synchronize using it. I would recommend using the ID3D11Multithread interface instead to guard access to the device context. This way other components can synchronize using the same mechanism (e.g. Media Foundation).

Having said all that, there were some bug fixes related to synchronization and the frame pool. I believe Windows 10 19045 should have them, but for sure Windows 11 will. Unfortunately my memory is a bit fuzzy with what released/shipped when.

robmikh avatar Jan 06 '24 22:01 robmikh