How can i update table when counter reach a specific value? (thread safe)
One update is atomic operation, so when running the second update twice, the second thread has to wait until the first finishes (when using innodb and transactions, entire transaction is atomic) and it should not update anything, because the click.count will be set to 0 before by the other thread.

But I see a potential problem with your code - if other thread updates count to 1001 too fast, your update won't do anything and will never repair it. You should probably do

SET click.count = click.count - 1000 ... WHERE click.count &gt;= 1000


so you can catch such cases. (They are maybe not very probable, but if you have lot of hits, then they can sure happen if you do not prevent them by some other means)