Thanks Daniel! Really appreciate the feedback, and thanks for catching that race condition. Unfortunately I'm still in a lab setting so haven't come across anything like that.
I will look at changing to use a callback style with variable arguments. For some reason at the time I didn't think this was an option but I can't see why this wouldn't work at the moment? And this would allow me to maintain the same lock all the way through.
I will work on this as soon as I get some time and come back to you. Thanks!