Resolve a question about pthread_rwlock_t
I had try to use pthread_rwlock_t to make a thread safe OperationCache. But I failed. I got a crash:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber objectForKey:]: unrecognized selector sent to instance 0x8000000000000000'
Find the reason. It was very import for me. I must confirm pthread_rwlock_t is valid to ensure thread safe.
step 0: make a mvp to reproduce this crash.
step 1: make some guesses
- maybe: I used pthread_rwlock_t in a wrong way.
- maybe: “read swift dict” is not thread safe.
step 2: verify my guess
- Luckily. When i google pthread_rwlock_t, I found the difference: I forgot to init pthread_rwlock_t. In honest, it was very strange. I thought lock had been init when i created it.
- now, follow code will not crash:
- I got a thread safe OperationCache tool.
- pthread_rwlock_t is reliable, but must be careful when use.