2026-01-06 01:01:21 Displayed 59 times

Temporary variables, copy constructors, and Pthread on macOS

Recently I re-discovered that macOS has support for pthread. I did try before but the only way out was to entirely disable pthread for macOS.

Then Claude-code arrived, and to my surprise it knew that copying a pthread_rwlock_t is undefined behavior according to POSIX. In my code I try to always use temporary variables before copying them to the actual pointer de-referenced. So this kind of works on BSD and Linux but not on Darwin. On Darwin to hold a pthread_mutex or a pthread_rwlock you have to call pthread_mutex_init or pthread_rwlock_init on the actual pointer. If you allocate to another memory address and then copy the pthread_*_t then you are in UB and all your pthread_mutex_lock and pthread_rwlock_wrlock will hang or (hopefully) return an error message "Invalid lock".

So after rewriting all my constructors to not copy from a temporary variable, now it will directly write to the destination address. It does not break any contract on the current code so it was trivial.