On Wed, 26 Jul 2023 20:56:15 GMT, Sergey Bylokhov <s...@openjdk.org> wrote:
> > Probably… But it is still incorrect. Initialising COM on a thread doesn't > > mean you can call COM object methods from any thread in your application. > > Note that "Objects created on a COM thread in a multithread apartment (MTA) > must be able to receive method calls from other threads at any time", so if > we initialisethe devices on one thread using COM they will be avaliable on > other threads as well. But from the threads where you initialised COM as MTA. The docs for `CoInitializeEx` clearly state you have to initialise COM before you can use it. Thus, calling methods of COM objects from an arbitrary thread in your application is wrong, isn't it? > But the spec for DirectSoundEnumerate says nothing about COM... But from some > examples I see ppl wrap it by the CoInitializeEx(NULL, > COINIT_MULTITHREADED)/CoUninitialize(); > > Noe sure we can call CoInitializeEx on the main thread. It's for any thread not only the main thread… You can all `AudioSystem.getMixerInfo` from any thread in a Java app, if `DirectSoundEnumerate` needs COM to be initialised even though the documentation doesn't mention it, we have to initalise COM. Thus there are two options: 1. Call `CoInitializeEx`, enumerate devices, and call `CoUninitialize`. 2. Create an executor service which initialises COM on its worker thread, enumerate devices on the worker thread and return the devices. The worker thread may terminate (after it calls `CoUninitialize`) if it doesn't receive more requests in a period of time. ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/14898#discussion_r1275489954