On Fri Jan 6 11:23:22 2023 +0000, Rémi Bernon wrote:
I'm not completely sure how this should be done, but you have a race condition here. For instance, if:
- StartAsync is called, setting worker_running to TRUE,
- StopAsync is called, setting worker_running to FALSE,
- session_stop_async is executed, not doing anything as the thread
isn't started, 4) session_start_async is executed, starting the thread, setting the thread handle. The thread will exit quickly as running is false, but you'll end up with an inconsistent state. I think it's perhaps better to do everything in the async callbacks, serialized with the critical section. Or otherwise in the method itself, and have the callbacks do nothing (not sure if asynchronicity really matters here).
What if I set `impl->worker_running = TRUE;` before the worker loop? That would mean it's only set when the worker is truly running. It would mean it runs at least one cycle, but I think that can be ignored?
```suggestion:-0+0 EnterCriticalSection(&impl->cs); impl->worker_running = TRUE; while (impl->worker_running) { /* TODO: Send mic data to recognizer and handle results. */
SleepConditionVariableCS(&impl->cv, &impl->cs, 500); /* Wait 500ms to slow down spinning for now. */ } LeaveCriticalSection(&impl->cs); ```