Add thread safe example

It's the version I ended up with, it probably can be improved, and should be helpful for people trying to embed the library in multithreaded applications.