Look for
optimistic concurrency. I'm sure there is support for this scenario in majority of data access technologies out there, e.g.:
Generally you compare the data that you have loaded in your app to the data in your database just before the update. In your example of buying pens this would go something like this:
update stock_table
set amount = @new_amount
where id = @pen_id
and amount = @old_amount
if @@ROWCOUNT = 0 RAISERROR(...)
This way you can be sure that nobody else update the row in the meantime. Or you can use a dedicated version column. But you definitely need to do some reading first.