Add test for pthread mutexes.
Make memory allocation thread-safe. Add test.