Error checking in thread code