Ruby:
- File in its current state
- Got a better understanding of mutexes and condition variables by playing around with the code
- Mutexes ensure that only one thread at a time executes a critical section of code, while condition variables can temporarily disable a mutex upon receiving a signal.
- This allows me to have a mutex around the statement which pops an item off of the queue, but temporarily release that mutex when a new item is added to the queue
- I also realized that I had an error in my logic. I was creating blocks of code to be executed with an incrementing variable saved in the block. However, blocks don't store their variables' values, but instead evaluate them when they are called.
- I was doing
i=0 loop do block = lambda { i } queue.push(block) i+=1 end
- However, by the time the block was called, the i variable had already changed from what I expected it to be
Cool! I can see why Ruby folks like their language.
ReplyDelete