Tonight we further studied the number-multiplying server example in Chapter 12. First we attempted to answer some of the questions the book posed, such as what happens when Ctrl+C is pressed on the client side, or the server side? This led us to experimenting with adding some debugging code to show what exceptions were being thrown and how they were propagated. We also talked about how to use readMay from the Safe library to check whether a string would decode into a number, rather than throwing an exception. And we looked at how we could open a log file using withFile, that would automatically be closed in case of an exception.
Then we studied the more advanced server2, in which a command could be sent to change the multiplier. Marlow discussed 4 different designs. The first one had much too much contention for a shared variable. The second design had less contention, but still had some when the multiplier was changed. The third design was efficient, if a bit complex, due to requiring three server threads and two channels per client. The fourth and final design used STM and only needed two server threads and one channel per client. We walked through the operation of each.
I pointed out that there was a subtle difference between designs 3 and 4: the fourth design, using STM, might not notice every time the multiplication factor changed. However, it could still guarantee that if it started multiplying numbers at a new factor, that it had previously notified its client of the new factor.
Next time we'll continue with the chat server example, to finish Chapter 12.