refactor code to use affinity library for thread lock