The lock expires with the end of the transaction. When a thread encounters a locked row, it blocks until the lock is released. Therefore only the first thread does the update, the next ones get the updated value and skip the update. Now thinking about it, it can be made simpler.

Please do not merge for now.