How to lock a thread on a particular data, while someone accessing same data?

I have a requirement. I have balance. That balance will use by multiple users same time. So how can i overcome that issue without any conflicts.

Example: I have 2 ATMs and withdrawing money from ATM same time. On that case. ATM will lock one transaction in First in Fist out manner. So same situation in my scenario. So how to overcome this issue on Couchbae synchgateway. Either i have internet connection or not.

You can’t do this directly in a distributed system. There are no transactions and there is no locking (otherwise one client could cause a DoS on the system by refusing to unlock.)

I don’t know exactly what your requirements are, but in general I would not store the balance directly. Instead I would have documents store individual transactions including the amount of the deposit/withdrawal. The balance can then be computed as the total of all the deposits/withdrawals, using a view query.