> You have a race condition here where if an item is enqueue, thread 2 can read > uninitialized memory, then thread 1 initializes the memory.
That is only true if you increment `len` prior to initializing memory on thread 1. > Any compiler will only read the length once and store it in register instead > of paying for N memory access. It's a basic optimization. Yes, so in either case: thread 1 will be not run into a "problematic" memory race in this scenario. The only possible scenario I can think of is if the compiler moves the `len` read outside of the iterator, e.g. var data = MyContainer() proc ioLoop() {.thread.} = while true: if dataCanBeRead: data.extend(newData) proc uiLoop() = while true: handleInput() block draw: for x in data: # if data.len is moved outside of the while, then we have a problem draw(x, ... position, etc. here) # ... start ioLoop in a seperate thread, let main thread be ui loop Run Is that possible? Iterators are inlined, so maybe?