Crashes on eunit test and on VM exit: the NIF thread / memory stuff is severely broken.

Hello, I have fixed the broken thread / memory handling logic in the NIF c_src.
Please review, check and pull.
Thx.