To fix the race condition I'd have to introduce a SPMC queue or an [asynchoronous ring buffer](https://ferrous-systems.com/blog/lock-free-ring-buffer/).