improve the logic of locking in the function
Merge pull request #1419 from sijchen/fix_sm4_mt

[Encoder] improve the logic of locking in the function