The suggestion that you should free the lock as soon as possible is WRONG (and for some reason, I can't downvote it right now).
Releasing the lock before the write() call is as effective as not using locks at all. The whole point is that a concurrent read() HAS to be blocked until the session is closed, otherwise you'll have race conditions.
If you care about the performance aspect, you should take care to call session_write_close() as soon as possible instead.