Apply changes to Linux and OS X

The previous stack calculation was actually only within the WIN32 part of the code.
This fixes that mistake and applies the changes to Linux and OS X.
In addition, used static thread_local to cache the pthread ID which saves 10% in CPU time.