The server code we developed in Section 25.2 is not quite correct as it stands: if two clients access the same file in parallel, each via a different thread, one thread may read the
_lines data member while another thread updates it. Obviously, if that happens, we may write or return garbage or, worse, crash the server. However, we can make the
read and
write operations thread-safe with a few trivial changes to the
FileI class:
We modified the constructor to add the instance member _mutex, and then enclosed our
read and
write implementations in a critical section. (The
name method does not require a critical section because the file’s name is immutable.)
No changes for thread safety are necessary in the DirectoryI class because the
Directory interface, in its current form, defines no operations that modify the object.