Fix thread guards to use thread IDs instead of handles (#1161)

GetCurrentThread() returns the same pseudo-handle for every thread. It cannot be used to determine if two calls were made from the same thread.