Fix issue #1705. - Use CAS to make it thread safe and limited in the declared range. Signed-off-by: cj <power4j@outlook.com>