[threads] Add checks around libc calls (#5240)