Modify the counter to make it works

New version has added some mutex lock. And original code is not working
because of some timeout issues.