Resolve a question about pthread_rwlock_t

SITUATION

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'

TASK

Find the reason. It was very import for me. I must confirm pthread_rwlock_t is valid to ensure thread safe.

ACTION

  • maybe: I used pthread_rwlock_t in a wrong way.
  • maybe: “read swift dict” is not thread safe.
  • 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:

Result

  • I got a thread safe OperationCache tool.
  • pthread_rwlock_t is reliable, but must be careful when use.

Related Information

iOS App Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Limit Concurrent Network Requests With DispatchSemaphore in Swift

Communicate with a Unity game embedded in a SwiftUI iOS App

SnapKit: Layout User Interface Programmatically with Swift 5

Implement a Model-View-Presenter Architecture in Swift 5

Perform network requests in your iOS app easier thanks to CombineNetworking

Accessing Realm Data on iOS Using Realm Studio

iOS testing — 4 crossroads technique — Part 1

The Best Way To Validate TextFields in Swift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Macro

Macro

iOS App Engineer

More from Medium

Preventing Invalid Characters from Being Entered in a UITextField

I finally used method-swizzling

RxSwift Memory Leak While Passing Functions to Operators

How to get started with Voice Over on iOS

An image of a microphone