kRPC: Control the game using C#, C++, Java, Lua, Python...

kRPC: Control the game using C#, C++, Java, Lua, Python...

7.8k Downloads

Wait on multiple streams

janismac opened this issue ยท 2 comments

commented

I'd like to wait on all updates from multiple streams for each physics tick. According to your comment here the updates are batched.

Is there a condition variable that notifies once all streams are updated?

Should I wait for all condition variables on all streams? Or would that be inefficient? (This is in Python)

commented

Thanks for the reply. I'll just use one condition variable and hope that the others have updated (it's not that critical). Looking forward to the new feature in the next version!

commented

There isn't a single condition variable for all updates - just one for each stream. Waiting on multiple condition variables won't really work - you would need to lock them in the correct order to avoid deadlock, but the lock order isn't apparent to the calling code.

A better way would probably be to use callbacks and a semaphore. Create a semaphore and initialize its value to the number of streams you want to wait on. Add a callback to each stream which decrements the semaphore. Then wait for the semaphore to reach 0, and when it does reset it.

You will need to be careful with this though - if one of the streams doesn't update with all the others (e.g. its value doesn't change since the last update) the callback won't be called, and the semaphore would reach 1. Then on the next set of stream updates the first stream to be updated would trigger the wait on the semaphore. In other words, then semaphore would get out of step! I can't think of a way to protect against this off the top of my head :(

Given this is a rather complex solution to what should be a simple use case, I think I might add a condition variable to the client library (in the next version) that is triggered when a stream update message is received from the server (i.e. when at least one stream has been updated).