Fix bad casts in thread intrinsics that broke some platforms

Contribution from Matteo Vescovi: fix for some platforms due to incorrect cast. This gets the last of the Debian platforms working!